根据我的约束,我无法设置UITableViewCell
的高度。
我在UITableView
和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
之前称为,我的标签已更新。
我没有更多想法来解决这个问题。
提前谢谢。
答案 0 :(得分:0)
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"indexPath.row :%ld",(long)indexPath.row);
CGSize contsize={260.00f,3000.00f};
UIFont *font=[UIFont fontWithName:@"Helvetica-Bold" size:15.0];
NSString *strt11=@"hellllllooooooooooooooooooo hellllllooooooooooooooooooo hellllllooooooooooooooooooo";
CGSize fontSize=[strt11 sizeWithFont:font constrainedToSize:contsize lineBreakMode:NSLineBreakByWordWrapping];
NSLog(@"fontSize indexPath :%f",fontSize.height);
if (fontSize.height>22)
return fontSize.height+50.0f;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"reuse"];
if(cell==nil)
{
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuse"];
UILabel *lblcomment=[[UILabel alloc] initWithFrame:CGRectMake(55,23, 160,20)];
[lblcomment setBackgroundColor:[UIColor clearColor]];
[lblcomment setTag:72];
[lblcomment setTextColor:[UIColor grayColor]];
[lblcomment setNumberOfLines:0];
[lblcomment setFont:[UIFont fontWithName:@"Helvetica" size:15.0]];
[lblcomment setText:@"Its rude day on goin...Its rude day on goin...Its rude day on goin..."];
[cell addSubview:lblcomment];
}
UILabel *lblcomment=(UILabel *)[cell viewWithTag:72];
CGSize contsize={260.00f,3000.00f};
UIFont *font=[UIFont fontWithName:@"Helvetica-Bold" size:15.0];
NSString *strt11=@"hellllllooooooooooooooooooo hellllllooooooooooooooooooo hellllllooooooooooooooooooo";
CGSize fontSize=[strt11 sizeWithFont:font constrainedToSize:contsize lineBreakMode:NSLineBreakByWordWrapping];
float h=20;
NSLog(@"re %f",fontSize.height);
if (fontSize.height>22.0f){
h=fontSize.height+7.0;
NSLog(@"heigtaaaaa %f",h);
}
[lblcomment setFrame:CGRectMake(55,23,260,h)];
NSString *strDecodeComment=@""hellllllooooooooooooooooooo hellllllooooooooooooooooooo hellllllooooooooooooooooooo";
[lblcomment setText:strDecodeComment];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
return cell;
}
答案 1 :(得分:0)
感谢Rohit的回答,请找到我的工作代码:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *content=((Comment *)[commentsArray objectAtIndex:indexPath.row]).content;
CGSize maximumLabelSize = CGSizeMake(234.00f, FLT_MAX);
CGRect expectedLabelRect = [content boundingRectWithSize:maximumLabelSize options:NSStringDrawingUsesLineFragmentOrigin attributes:nil context:nil];
if (expectedLabelRect.size.height > 41) {
return expectedLabelRect.size.height + 81;
}
else
return 81;
}