• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

iOS 自动布局 : Display the cell only after its contents are updated

[复制链接]
菜鸟教程小白 发表于 2022-12-12 18:24:57 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我使用自动布局并显示带有自定义动态单元格的表格。基本上表格显示 2 人之间的聊天。因此,每个单元格的文本消息各不相同。

这里的问题是首先显示单元格,然后在一秒钟内调整其内容的大小。这很明显,看起来很糟糕。

查看下图了解调整大小前后的外观。

调整大小之前:

enter image description here

调整大小后:

enter image description here

我知道有一个 similar question关于 SO,但它的答案并不能真正满足需求。我想避免覆盖 layoutSubviews() 方法,因为我认为它不是一个足够好的解决方案。

我尝试使用下面的代码在单元格的 contentView 显示后取消隐藏它们。但它不起作用。

- (void)tableViewUITableView *)tableView willDisplayCellUITableViewCell *)cell forRowAtIndexPathNSIndexPath *)indexPath
{
   // At the begining
    cell.contentView.hidden = NO;
}


- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath
{
    ...
    // Before returning cell
    cell.contentView.hidden = YES;
    return cell;
}

有没有办法延迟显示单元格,直到调整大小以适合其内容?

更新:

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath
{
    chatCell *cell = (chatCell *)[tableView dequeueReusableCellWithIdentifier:CHAT_CELL_IDENTIFIER];

        cell.textString.text = ...;
        cell.textString.frame = ...;
        cell.timeLabel.text = ...;                                             // set timeLabel to display date and time

        cell.userLabel.text = ...;       // set userLabel to display userName

        if (displaying cell where message is sent by user)
        {
            // Set image for sender
            cell.chatCellBackground.image = [[UIImage imageNamed"bubbleMine.png"] stretchableImageWithLeftCapWidth:STRETCHED_WIDTH topCapHeight:STRETCHED_HEIGHT];

            cell.chatCellBackground.frame = ...;

        }
        else
        {
            // set bubble for receiver
            cell.chatCellBackground.image = [[UIImage imageNamed"bubbleSomeone.png"] stretchableImageWithLeftCapWidth:STRETCHED_WIDTH topCapHeight:STRETCHED_HEIGHT];

            cell.chatCellBackground.frame = ...;
        }
    }

    [cell setNeedsLayout];
    [cell layoutIfNeeded];
    return cell;
}



Best Answer-推荐答案


我在自定义 UITableViewCell 的类中添加了以下方法。

- (void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat maxTextWidth = [UIScreen mainScreen].bounds.size.width * 0.40;
    self.userLabel.preferredMaxLayoutWidth = maxTextWidth;
    self.chatMessage.preferredMaxLayoutWidth = maxTextWidth;
    self.timeLabel.preferredMaxLayoutWidth = self.timeLabel.frame.size.width;
    [super layoutSubviews];
}

我还发现在

中返回完全正确的单元格高度
- (CGFloat)tableViewUITableView *)tableView heightForRowAtIndexPathNSIndexPath *)indexPath

方法很重要。

提示:

  1. 如果任何 View 的高度大于预期,则计算出的单元格高度可能大于实际需要的高度。
  2. 如果任何 View 垂直缩小或不显示全部内容,则计算出的单元格高度可能小于实际需要的高度。

Yoy 可以通过向您为单元格计算的高度(变量)添加/删除常数值来测试高度是否错误。

关于iOS 自动布局 : Display the cell only after its contents are updated,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21457871/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap