ios - 具有动态高度的 UITableView 行在 IOS 8 中工作,在 iOS 7 中不工作

标签 ios iphone xcode uitableview autolayout

我有一个表格 View ,我需要在其中填充具有动态高度的行。

我做了什么:

我有一个高度会动态变化的文本标签。所以当我在我的单元格上设置数据时,我将其设置为:

-(void)setCommentDataOnCell:(CommentModel*) data
{


    CGFloat currentHeigh=_commentTextLabel.frame.size.height,heightDiff;

//Here is the label with dynamic height

    _commentTextLabel.text=data.commentText;
    [_commentTextLabel sizeToFit];
    [self layoutIfNeeded];
    heightDiff=_commentTextLabel.frame.size.height-currentHeigh;

// I change the frame of cell here ie, add the height difference in label

    [self setFrame:CGRectMake(self.contentView.frame.origin.x, self.contentView.frame.origin.y, self.contentView.frame.size.width, self.contentView.frame.size.height+heightDiff)];
    [self layoutIfNeeded];

    commentAuthorLabel.text=data.createdUserName;

}

现在我已经设置了自动布局,使动态单元格与单元格内容 View 的底部和顶部对齐

enter image description here

然后我为我的 tableview 设置了以下属性

commentListTableView.rowHeight=UITableViewAutomaticDimension;
commentListTableView.estimatedRowHeight=44.0f;

现在我得到了什么:

*** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[DiscussionDetailViewController tableView:heightForRowAtIndexPath:]: 无法识别的选择器发送到实例 0x78787760”


我还没有实现这个选择器 heightForRowAt

但是我已经将 EstimatedHeight 实现为 75.0f 因为它是我 Storyboard中的原型(prototype)单元格我还有一个高度为 8.0f 的 session 标题 View

最佳答案

您需要实现 heightForRowAtIndexPath 方法以支持 iOS 7。

请引用这个答案:

https://stackoverflow.com/a/18746930/1282896

关于ios - 具有动态高度的 UITableView 行在 IOS 8 中工作,在 iOS 7 中不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27200133/

相关文章:

ios - ARKit 2 - 裁剪识别图像

c - 在 XCode 中复制目标时,新文件中的更改仍然会出现在旧文件中

iphone - 捕捉 UINavigationController native 后退按钮操作

ios - 如何在 uitableview admob swift 中显示广告?

ios - 无法使用 UIActivityViewController 在 Facebook 中共享屏幕截图和 URL

ios - OCMock 和 UIViewController

iphone - iPhone 上使用 obj-c 时,自动释放所有内容而不是释放有什么坏处吗?

iphone - 机芯不工作

iphone - 需要 FTP 在 iPhone 中上传示例代码或教程

iphone - 如何将值添加到 fb post xcode