iOS UITableViewCell 自动调整大小错误

标签 ios objective-c iphone uitableview

我有一个使用 .xib 设计的 UITableViewCell,它使用单元格自动调整大小。在 iOS 9 中运行良好,但在 iOS 8 中,单元格不会自行扩展,并且内部标签仍保留 1 行文本。如果单元格离开屏幕并返回(即滚动后),则所有标签均正常。

想法?我认为这是一个 iOS 8 错误。

enter image description here enter image description here

最佳答案

我遇到过同样的问题。如果您的约束设置正确,您需要在返回单元格之前为单元格中的每个 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/

相关文章:

ios - 是什么决定了多个 UITableView 的加载顺序?

ios - Firebase 匿名登录 - 为什么 FirebaseID 在登录到 Facebook 时发生变化,但在未登录到 Facebook 时保持不变?

ios - Swift 2.1 - 删除单元格后清除 TableViewCell 中的文本字段

ios - 对 block 内弱引用的强引用

objective-c - 在 NSMenuItem 上设置标题,没有效果

iphone - 将对象设置为 nil 与在 dealloc 中向其发送释放消息有什么区别

ios - 在 UICollectionView 中,我如何预加载 cellForItemAtIndexPath 之外的数据以在其中使用?

ios - 如何在不快速继承 NSObject 的情况下覆盖 NSObject 方法

iphone - AsyncSocket 不调用委托(delegate)

iphone - 获取UIImage的像素颜色