更改每个实例的UITableViewCell宽度约束的常量值

我正在尝试创build一个聊天应用程序,在那里我只使用代码做了一个自定义的UITableViewCell。 所以就像泡泡里面的名字,消息和时间标签一样, 名称标签应该是总气泡宽度的一半,气泡宽度由最大宽度为220.0f的消息的宽度决定,之后它将转到下一行。

我面临的问题是:我试图根据消息宽度更改名称标签的宽度约束的常量。 但是由于iOS重用了这个单元格,当我滚动我的UITableView的时候,一些名称标签的宽度会变得混乱。 它试图使用旧的宽度,因此,如果名称足够大,名称标签将从泡泡中跳出。

我附上一些图像来certificate:

正确的名称标签宽度http://postimg.org/image/yd6z2jdft/c1f192cd/

名称标签由于滚动而产生错误的宽度http://postimg.org/image/u0m7pc8uh/cd7ea4ea/

这是我正在使用的代码。 我只发布相关部分

的cellForRowAtIndexPath:

chatCell = (ChatTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"chatSend"]; if (chatCell == nil) { chatCell = [[ChatTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"chatSend"]; } chatCell.chatMessageLabel.text = message.getMessage; chatCell.chatNameLabel.text = message.getFromName; chatCell.chatTimeLabel.text = [dateFormatter stringFromDate:messageDateTime]; [chatCell setChatNameLabelWidth:Messagesize.width]; 

Heightforrowatindexpath:

 Messagesize = [message.getMessage boundingRectWithSize:CGSizeMake(220.0f, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:14]} context:nil].size; 

ChatTableViewCell.m

在课堂延伸

 @property (nonatomic, strong) NSLayoutConstraint *chatNameLabelWidthConstraint; 

init方法

 horizontal = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-16-[chatNameLabel]" options:NSLayoutFormatDirectionLeftToRight metrics:nil views:NSDictionaryOfVariableBindings(chatNameLabel)]; [Main addConstraints:horizontal]; // //////////////////////////////////////////////////////////////////////////////////////////// //Setting width constraint for chatNameLabel chatNameLabelWidthConstraint = [NSLayoutConstraint constraintWithItem:chatNameLabel attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.f constant:64.0f]; [chatNameLabel addConstraint:chatNameLabelWidthConstraint]; // //////////////////////////////////////////////////////////////////////////////////////////// //Setting the constraints for chatNameLabel. It should be at 16 distance from right and left of superview, ie, Main and 8 distance from top and chatMessageLabel which is at 8 distance from chatTimeLabel which is at 8 distance from bottom of superview. vertical = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-8-[chatNameLabel]-8-[chatMessageLabel]-8-[chatTimeLabel]-8-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(chatNameLabel,chatMessageLabel,chatTimeLabel)]; [Main addConstraints:vertical]; 

另一种设置宽度的方法

 - (void)setChatNameLabelWidth:(CGFloat) messageWidth { CGFloat chatNameLabelWidth; if((messageWidth + 32.0f)<128.0f) { chatNameLabelWidth = 64.0f; } else { chatNameLabelWidth = (messageWidth + 32.0f)/2; } chatNameLabelWidthConstraint.constant = chatNameLabelWidth; [chatNameLabel layoutIfNeeded]; } 

最后,我用比例宽度解决了它。 如果使用约束条件,一定会被搞乱,但是使用比例宽度以及内容拥有和内容压缩阻力的优先级,应该很容易解决问题:

这是我做到的:

 NSLayoutConstraint *proportionalWidth = [NSLayoutConstraint constraintWithItem:chatNameLabel attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:Main attribute:NSLayoutAttributeWidth multiplier:.5 constant:0]; proportionalWidth.priority = 750; [Main addConstraint:proportionalWidth]; [chatNameLabel setContentCompressionResistancePriority:250 forAxis:UILayoutConstraintAxisHorizontal];