我有一个使用 .xib
设计的 UITableViewCell
,它使用单元格自动调整大小。在 iOS 9 中运行良好,但在 iOS 8 中,单元格不会自行扩展,并且内部标签仍保留 1 行文本。如果单元格离开屏幕并返回(即滚动后),则所有标签均正常。
想法?我认为这是一个 iOS 8 错误。
最佳答案
我遇到过同样的问题。如果您的约束设置正确,您需要在返回单元格之前为单元格中的每个 UILabel
设置 preferredMaxLayoutWidth
。这是一些示例代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
cell.lblTitle.text = @"N";
cell.lblDetails.text = @"bla bla";
cell.lblOther.text = @"other text";
// Configure the cell...
CGfloat leftPading =// "your logo width + spacing between logo and label"
CGfloat rightPading = //your label trailing space
cell.lblTitle.preferredMaxLayoutWidth = CGRectGetWidth(tableView.bounds)-(leftPading +rightPading);
cell.lblDetails.preferredMaxLayoutWidth = CGRectGetWidth(tableView.bounds)-(leftPading +rightPading);
[cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];
return cell;
}
Have a look at this to know how to put constrains properly
希望对您有所帮助。
关于iOS UITableViewCell 自动调整大小错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35267099/