gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:40:36 25 4
gpt4 key购买 nike

我正在尝试创建一个聊天应用程序,其中我只使用代码创建了一个自定义 UITableViewCell。所以它就像一个气泡,气泡内的名称、消息和时间标签以相同的顺序排列。名称标签应为气泡总宽度的一半,气泡的宽度由消息的宽度决定,最大宽度为 220.0f,之后将转到下一行。

我面临的问题是:我正在尝试根据消息宽度更改名称标签的宽度约束常量。但是由于 iOS 重复使用单元格,当我滚动我的 UITableView 时,一些名称标签的宽度被弄乱了。它会尝试使用旧宽度,因此,如果名称足够大,名称标签就会脱离气泡。

我附上一些图片来证明:

名称标签的正确宽度 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];

索引路径的高度:

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;

初始化方法

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, i.e., 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];

关于ios - 为每个实例更改 UITableViewCell 的宽度约束的常量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32435425/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com