根据UITableViewCell中的“UILabel”文本更改“UIView”的宽度

在我的UITableViewCell里面有宽度约束的UIView,里面有Image和UILabel。

现在我想根据UILabel宽度更改主UIView宽度&为此,我已经尝试下面的代码。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { VideoCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"VideoCommentCell"]; CommentObject *objComment = [arrChatData objectAtIndex:indexPath.row]; if (cell == nil) { cell = [[[NSBundle mainBundle] loadNibNamed:@"VideoCommentCell" owner:self options:nil] objectAtIndex:0]; } cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.transform = CGAffineTransformMakeRotation(M_PI); cell.lblUsername.text = [NSString stringWithFormat:@"@%@", objComment.strCommentatorName]; cell.lblComment.text = objComment.strComment; cell.vwBack.tag = indexPath.row; if (![arrAlreadyChangeCellFrames containsObject:[NSNumber numberWithInteger:objComment.nCommentID]]) { float usernameWidth = [cell.lblUsername.text boundingRectWithSize:cell.lblUsername.frame.size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName:cell.lblUsername.font } context:nil] .size.width; float commentWidth = [cell.lblComment.text boundingRectWithSize:cell.lblComment.frame.size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName:cell.lblComment.font } context:nil] .size.width; NSLog(@"UserName Width %f", usernameWidth); NSLog(@"Comment Width %f", commentWidth); if (usernameWidth > commentWidth) { cell.constraintWidthOfMainBackVW.constant = usernameWidth + 53; } else { cell.constraintWidthOfMainBackVW.constant = commentWidth; } [arrAlreadyChangeCellFrames addObject:[NSNumber numberWithInteger:objComment.nCommentID]]; } else { } cell.imgUserProfilePic.layer.cornerRadius = cell.imgUserProfilePic.frame.size.width / 2.0f; cell.imgUserProfilePic.clipsToBounds = YES; if (objComment.nCommentatorID == [SharedClass objSharedClass].currentUser.nUserID) { [cell.imgUserProfilePic sd_setImageWithURL:[NSURL URLWithString:[SharedClass objSharedClass].currentUser.strUserImage] placeholderImage:[UIImage imageNamed:kUserPlaceHolderImage]]; } else { [cell.imgUserProfilePic sd_setImageWithURL:[NSURL URLWithString:objComment.strCommentatorImage] placeholderImage:[UIImage imageNamed:kUserPlaceHolderImage]]; } cell.lblTimeStamp.text = [[SharedClass objSharedClass] convertDateStringToUTCDate:objComment.strCommentTime]; return cell; } 

它工作正常,但问题是当我滚动tableview,所有的UIView宽度搞砸了或没有正确设置。

所以你可以请build议我如何解决这个问题?

截图 在这里输入图像说明

我上传我的演示项目链接https://www.dropbox.com/s/kjwfyg6zu04uzc4/CommentDemo.zip?dl=0

提前致谢。

添加你的视图内的堆栈视图和视图内添加您的标签和imageview,并设置适当的约束标签和imageview。 并删除所有代码来计算大小。

按照下面的截图。

在这里输入图像说明

添加这个viewDidLoad

 tblView.estimatedRowHeight = 100; tblView.rowHeight = UITableViewAutomaticDimension; 

使用stackView 。 你不必考虑任何事情。

在这里输入图像说明

在这里输入图像说明

删除arrAlreadyChangeCellFrames 。 为一个对象设置一次的单元格可以重置为不同的对象,因此必须调整其大小。

Tableviews和Collectionview通过重用单元格来工作。 所以,即使你滚动的速度非常快,内存中只有less数几个单元格。 正因为如此,在cellForRowAtIndexPath总是遵循一个非常重要的规则: 每个if必须有一个else 。 因为单元格可以被重用,所以你在一个案例中设置的任何东西都必须在另一个案例中取消设置。

另外,对于大多数布局,当文本更改时,不需要设置标签的宽度。 只要将宽度设置为允许的最大尺寸,文本将尽可能多地填满。

非常感谢KKRocks的回答,需要在该答案中添加1个事项,只需将StackView Trailing约束的优先级更改为> =,并按照subContent压缩StackView。