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.
Naprawiono go przez ponowne załadowanie tabeli w didRotateFromInterfaceOrientation: ale wciąż szukam czegoś działającego bez konieczności ponownego ładowania tabeli. – Imotep