iOS UITableViewCell自动调整错误

时间:2016-02-08 10:15:08

标签: ios objective-c iphone uitableview

我使用UITableViewCell设计的.xib使用了细胞自动调整功能。在iOS 9中运行良好,但在iOS 8中,单元格不会自行扩展,内部标签仍然保留1行文本。如果单元格离开屏幕并且返回(即在滚动之后)所有标签都可以。

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

enter image description here enter image description here

3 个答案:

答案 0 :(得分:3)

我遇到了同样的问题。如果您的约束设置正确,则需要在返回单元格之前为单元格中的每个preferredMaxLayoutWidth设置UILabel。这是一些示例代码

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

希望这会对你有所帮助。

答案 1 :(得分:-1)

这取决于你的代码,但有一个非常简单的方法来做到这一点

1. Define auto layout constraints for your prototype cell
2. Specify the estimatedRowHeight of your table view
3. Set the rowHeight of your table view to UITableViewAutomaticDimension

可在此处找到一个很好的解释http://www.appcoda.com/self-sizing-cells/

编辑:好吧,我猜这个笑话是我假设问题是关于swift而不是obj-c(没注意到标签)。 因为我认为我的答案仍然有用(对于其他人而言)......我将把它留在这里。

答案 2 :(得分:-1)

您需要为单元格的高度添加表视图委派方法。

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}

- (CGFloat) tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50.0f;
}

它会起作用。如果有任何问题可能会对自动布局约束产生影响。