【问题标题】:Changing UITableViewCell's width constraint's constant value for each instance为每个实例更改 UITableViewCell 的宽度约束的常量值
【发布时间】:2015-09-07 09:35:44
【问题描述】:

我正在尝试创建一个聊天应用程序,其中我只使用代码制作了一个自定义 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];

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;

初始化方法

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];
}

【问题讨论】:

    标签: ios uitableview variables constraints programmatically-created


    【解决方案1】:

    最后,我使用比例宽度解决了这个问题。如果使用约束,它肯定会搞砸,但使用比例宽度以及内容拥抱和内容压缩阻力优先级,应该很容易解决问题:

    我就是这样做的:

    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];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多