2014-11-17 12 views
6

Ho un UITableViewCell con un UIImage fisso in alto a sinistra, e un'etichetta alla sua destra:UITableViewCell systemLayoutSizeFittingSize: ritorno 0 su iOS 7

-(UITableViewCell*) adCellFromTableView:(UITableView*)tableView 
{ 
    //Build the text 
    NSString* adText = NSLocalizedString(@"MyFamily_fremiumAd", nil); 
    NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString:adText]; 

    NSUInteger newLineLocation = [adText rangeOfCharacterFromSet:[NSCharacterSet newlineCharacterSet]].location; 

    //Set the first line in orange 
    NSDictionary* firstLineAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:15], 
              NSForegroundColorAttributeName:ORANGE}; 
    [attrString addAttributes:firstLineAttributes range:NSMakeRange(0, newLineLocation)]; 
    //Set other lines in white 
    NSDictionary* otherLinesAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:11], 
              NSForegroundColorAttributeName:[UIColor whiteColor]}; 
    [attrString addAttributes:otherLinesAttributes range:NSMakeRange(newLineLocation, adText.length - newLineLocation)]; 

    //Get the cell 
    if (!adCell) 
    { 
     adCell = [tableUsers dequeueReusableCellWithIdentifier:@"fremiumAd"]; 
    } 

    //Set the text 
    UILabel* label = (UILabel*)[adCell viewWithTag:1]; 
    label.attributedText = attrString; 

    //Hide the separator 
    adCell.separatorInset = UIEdgeInsetsMake(0, adCell.bounds.size.width, 0, 0); 

    return adCell; 
} 


-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
UITableViewCell* cell = [self adCellFromTableView:tableView]; 

      //Make sure the cell's bounds are the same as tableview's before calculating the height 
      //This is important when we calculate height after a UI rotation. 
      cell.bounds = CGRectMake(0, 0, tableView.bounds.size.width, 0); 
      NSLog(@"cell.bounds: %@",NSStringFromCGRect(cell.bounds)); 
      [cell setNeedsLayout]; 
      [cell layoutIfNeeded]; 

      CGSize fittingSize = [cell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; 
      NSLog(@"fittingSize: %@",NSStringFromCGSize(fittingSize)); 
      return fittingSize.height; 
} 

Quando eseguo l'applicazione su iOS 7.1 simulatore, systemLayoutSizeFittingSize sempre return 0:

cell.bounds: {{0, 0}, {320, 0}}

fittingSize: {0,0}

Quando eseguo l'applicazione su iOS 8.1 simulatore, systemLayoutSizeFittingSize restituire un valore corretto:

cell.bounds: {{0, 0}, {320, 0}}

fittingSize: {320 , 154.5}

Cosa mi manca?

Edit: Kinda risolto il problema utilizzando [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; invece di [cell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];

Ma questo è solo un mezzo correzione: quando ho ruotare mentre la cella è visibile, il calcolo dimensione è OK. Ma quando ho scorrere la cellula fuori dallo schermo, ruotare l'interfaccia utente e quindi scorrere indietro alla cella, il calcolo altezza è di nuovo sbagliato in iOS 7.1

Qui ci sono i registri prima di ruotare da orizzontale a verticale:

Tableview limiti: {{0, 0}, {569, 227}}

cell.bounds: {{0, 0}, {569,0}}

cell.contentView: {{0, 0}, {569, 0}}

fittingSize: {559, 162}

Qui ci sono i log dopo la rotazione da orizzontale a verticale:

tableView limiti: {{0, 249}, {321, 463}}

cell.bounds: {{0, 0}, {321,0}}

cell.contentView: {{0, 0}, {321, 0}}

fittingSize: {559, 162}

Come si può vedere, il calcolo delle dimensioni è uguale, indipendentemente dalla larghezza di cell/cell.contentView.

Questo risultato in una cella sovradimensionata quando si ruota da verticale a orizzontale e una cella sottodimensionata quando si ruota da orizzontale a verticale.

+0

Infine è stato risolto ricaricando il tavolo in didRotateFromInterfaceOrientation: Ma io sono ancora alla ricerca di qualcosa di lavoro senza dover ricaricare la tabella. – Imotep

risposta

0

Hai ragione che usi cell.contentView invece di cella in systemLayoutSizeFittingSize: method. Utilizzando il layout automatico, tutte le visualizzazioni secondarie personalizzate devono essere aggiunte al contentView della cella e ai vincoli. Quindi le celle di layout automatico funzionano bene come previsto.

Come seconda domanda, ho risolto un problema simile. Inserisci il seguente codice nel tuo - (void) didRotateFromInterfaceOrientation: (UIInterfaceOrientation) dal metodo InterfaceOrientation, forse può aiutarti a risolvere questo problema.

-(void) didRotateFromInterfaceOrientation: (UIInterfaceOrientation)fromInterfaceOrientation 
{ 
    // Update the layout for the new orientation 
    //Maybe you should change it to your own tableview 
     [self updateViewConstraints]; 
     [self.view layoutIfNeeded]; 
    // other any code 
    ... 

}