我有一个表格 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 的底部和顶部对齐
然后我为我的 tableview 设置了以下属性
commentListTableView.rowHeight=UITableViewAutomaticDimension;
commentListTableView.estimatedRowHeight=44.0f;
现在我得到了什么:
*** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[DiscussionDetailViewController tableView:heightForRowAtIndexPath:]: 无法识别的选择器发送到实例 0x78787760”
我还没有实现这个选择器 heightForRowAt
但是我已经将 EstimatedHeight 实现为 75.0f 因为它是我 Storyboard中的原型(prototype)单元格我还有一个高度为 8.0f 的 session 标题 View
最佳答案
关于ios - 具有动态高度的 UITableView 行在 IOS 8 中工作,在 iOS 7 中不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27200133/