2014-11-17 21 views
6

Mam UITableViewCell z UIImage ustalonej w lewym górnym rogu, a także etykiety na jej prawej:UITableViewCell systemLayoutSizeFittingSize: wracając 0 na 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; 
} 

Kiedy uruchomić aplikację na iOS 7.1 symulator, systemLayoutSizeFittingSize zawsze zwraca 0:

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

fittingSize {0,0}

Po uruchomieniu aplikacji na iOS 8,1 symulatora systemLayoutSizeFittingSize zwrócić właściwą wartość:

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

fittingSize {320 , 154.5}

Czego mi brakuje?

Edit: Jakbym rozwiązaniu problemu przy użyciu [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; zamiast [cell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];

Ale to tylko połowa fix: kiedy obracać podczas gdy komórka jest widoczna, obliczenie wielkości jest OK. Ale kiedy przewijać komórkę z ekranu, obróć UI, a następnie wskaż z powrotem do celi, obliczenie wysokości jest nie tak znowu w iOS 7.1

Oto logi przed obróceniem z poziomego na pionowy:

tableview granice: {{0, 0}, {569, 227}}

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

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

dopasowanie rozmiar: {559, 162}

Oto kłody po obróceniu z poziomej na pionową:

Tableview zakresem: {0 {249}, {321, 463}}

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

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

fittingSize {559, 162}

Jak widać, obliczenie rozmiaru jest takie samo, bez względu na szerokość komórki/komórki.contentView.

Ten wynik w przewymiarowanej komórce podczas obracania z pionowej na poziomą i niewymiarowej komórki podczas obracania z pionowej do pionowej.

+0

Naprawiono go przez ponowne załadowanie tabeli w didRotateFromInterfaceOrientation: ale wciąż szukam czegoś działającego bez konieczności ponownego ładowania tabeli. – Imotep

Odpowiedz

0

Masz rację, że użyj cell.contentView zamiast komórki w systemLayoutSizeFittingSize: method. Korzystając z funkcji automatycznego układu, wszystkie niestandardowe widoki podrzędne powinny być dodawane tylko do zawartości komórki w widoku, a także w wiązaniach. Następnie komórki Auto Layout działają dobrze zgodnie z oczekiwaniami.

Jako drugie pytanie, rozwiązałem podobny problem. Wstaw następujący kod w - (void) didRotateFromInterfaceOrientation: (UIInterfaceOrientation) fromInterfaceOrientation method może pomóc rozwiązać ten problem.

-(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 
    ... 

} 
Powiązane problemy