后调整高度的UITableViewCell我可以根据我的约束没有设定我UITableViewCell
的高度。的cellForRowAtIndexPath
我在每个单元格上都有我的UITableView
a UILabel
其中高度不固定并取决于此高度的这个UILabel
。
我试图设置在框架上电池后,我更新标签的内容,但没有任何反应。请在下面找到我的代码:
//On my controller
- (UITableViewCell *)tableView:(UITableView *)tView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CommentCell";
CommentCell *commentCell = [tView dequeueReusableCellWithIdentifier:CellIdentifier];
if (commentCell == nil) {
commentCell = [[CommentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Comment *comment;
if (commentsArray.count > indexPath.row) {
comment = [commentsArray objectAtIndex:indexPath.row];
}
[commentCell setComment:comment];
[commentCell setCommonFields];
currentCellheight = commentCell.contentLabel.frame.size.height + 40;
DDLogInfo(@"Current Cell Height: %f", currentCellheight);
return commentCell;
}
//On my custom Cell (CommentCell.m)
-(void)setCommonFields {
if (self.comment != nil) {
self.contentLabel.text = self.comment.content;
self.contentLabel.numberOfLines = 0;
CGSize maximumLabelSize = CGSizeMake(self.contentLabel.frame.size.width, FLT_MAX);
CGRect expectedLabelRect = [self.contentLabel.text boundingRectWithSize:maximumLabelSize options:NSStringDrawingUsesLineFragmentOrigin attributes:nil context:nil];
CGRect newFrame = self.contentLabel.frame;
newFrame.size.height = expectedLabelRect.size.height;
self.contentLabel.frame = newFrame;
}
}
我想也设置高度感谢heightForRowAtIndexPath
方法,但这种方法cellForRowAtIndexPath
在我的标签更新之前称为。
我没有更多的想法来解决这个问题。
预先感谢您。
没有魔法,你需要确定在'heightForRowAtIndexPath'方法你的身高和返回。你不能设置单元格的高度'cellForRowAtIndexPath' – Adam