我有一个 UITableViewCell,其左上角固定有一个 UIImage,右侧有一个标签:
-(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;
}
当我在 iOS 7.1 模拟器上运行应用程序时,systemLayoutSizeFittingSize 总是返回 0:
cell.bounds: {{0, 0}, {320, 0}}
fittingSize: {0,0}
当我在 iOS 8.1 模拟器上运行应用程序时,systemLayoutSizeFittingSize 返回一个正确的值:
cell.bounds: {{0, 0}, {320, 0}}
fittingSize: {320, 154.5}
我错过了什么?
编辑:
我使用 [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
而不是 [cell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
但这只是修复了一半:当我在单元格可见的情况下旋转时,大小计算就可以了。但是当我将单元格滚动到屏幕外,旋转 UI 然后滚动回单元格时,在 iOS 7.1 中高度计算又是错误的
这是从横向旋转到纵向之前的日志:
tableView bounds: {{0, 0}, {569, 227}}
cell.bounds : {{0, 0}, {569,0}}
cell.contentView: {{0, 0}, {569, 0}}
fittingSize: {559, 162}
这是从横向到纵向旋转后的日志:
tableView bounds: {{0, 249}, {321, 463}}
cell.bounds : {{0, 0}, {321,0}}
cell.contentView: {{0, 0}, {321, 0}}
fittingSize: {559, 162}
如您所见,无论 cell/cell.contentView 的宽度是多少,尺寸计算都是一样的。
当从纵向旋转到横向时,这会导致单元格过大,而当从横向旋转到纵向时,单元格会过小。
最佳答案
您是对的,在 systemLayoutSizeFittingSize: 方法中使用 cell.contentView 而不是 cell。 使用自动布局,所有自定义 subview 都应该只添加到单元格的 contentView 中,以及约束。然后 Auto Layout 单元格将按您的预期正常工作。
作为你的第二个问题,我已经解决了类似的问题。在您的 -(void) didRotateFromInterfaceOrientation :(UIInterfaceOrientation)fromInterfaceOrientation 方法中插入以下代码也许可以帮助您解决这个问题。
-(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
...
}
关于ios - UITableViewCell systemLayoutSizeFittingSize : returning 0 on iOS 7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26977680/