iOS的自动布局:显示其内容被更新后才细胞 [英] iOS auto layout : Display the cell only after its contents are updated

查看:125
本文介绍了iOS的自动布局:显示其内容被更新后才细胞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用自动布局和显示自定义动态单元格的表。基本上表显示两个人之间的聊天。因此,对于每个小区文本消息而变化。

这里的问题在于,第一单元的显示,然后在一秒钟内,其内容被调整大小。这是清晰可见,看起来不错。

请参阅下面的图像来获得它的外观和之前调整大小后的想法。

调整前:

调整后:

我知道有一个<一个href=\"http://stackoverflow.com/questions/20925247/setneedslayout-relayouts-the-cell-only-after-is-has-been-displayed\">similar问题SO 的,但它的答案并不能真正满足需要。我想避免重写 layoutSubviews()方法,我不认为这是一个足够好的解决方案。

我试过低于code取消隐藏单元格的内容查看一旦被显示出来。但是,这是行不通的。

   - (空)的tableView:(UITableView的*)的tableView willDisplayCell:(*的UITableViewCell)单元forRowAtIndexPath:(NSIndexPath *)indexPath
{
   // 在开始时
    cell.contentView.hidden = NO;
}
- (UITableViewCell的*)的tableView:(UITableView的*)的tableView的cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ...
    //返回前细胞
    cell.contentView.hidden = YES;
    返回细胞;
}

有延缓细胞显示,直到它们的大小以适合其内容的一种方式?

更新:

   - (UITableViewCell的*)的tableView:(UITableView的*)的tableView的cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    chatCell *电池=(chatCell *)的tableView dequeueReusableCellWithIdentifier:CHAT_CELL_IDENTIFIER];        cell.textString.text = ...;
        cell.textString.frame = ...;
        cell.timeLabel.text = ...; //设置timeLabel显示日期和时间        cell.userLabel.text = ...; //设置userLabel显示的userName        如果(显示单元,其中消息是由用户发送)
        {
            //发件人设置图像
            cell.chatCellBackground.image = [UIImage的imageNamed:@bubbleMine.png] stretchableImageWithLeftCapWidth:STRETCHED_WIDTH topCapHeight:STRETCHED_HEIGHT];            cell.chatCellBackground.frame = ...;        }
        其他
        {
            //设置泡接收器
            cell.chatCellBackground.image = [UIImage的imageNamed:@bubbleSomeone.png] stretchableImageWithLeftCapWidth:STRETCHED_WIDTH topCapHeight:STRETCHED_HEIGHT];            cell.chatCellBackground.frame = ...;
        }
    }    [细胞setNeedsLayout]
    [细胞layoutIfNeeded]
    返回细胞;
}


解决方案

我在下面的方法定制的类。的UITableViewCell

   - (空)layoutSubviews
{
    [超级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。
    [超级layoutSubviews]
}

我还发现,在

返回细胞的完全正确的高度

   - (CGFloat的)的tableView:(UITableView的*)的tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

方法是至关重要的。

提示:


  1. 如果任何观点的高度大于预期的可能,然后计算出细胞的高度比实际需要更多的。

  2. 如果任何意见正在缩小垂直或不那么显示全部内容细胞可能计算高度比实际需要较少。

同比可以测试是否高度错了添加/删除常量值,高度(变量),你计算的单元格。

I use auto layout and display a table with custom dynamic cells. Basically table displays chat between 2 persons. Thus, text message varies for each cell.

The issue here is that first cells are displayed and then within a second, its contents are resized. This is clearly visible and looks bad.

See below images to get idea of how it looks before and after resizing.

Before resizing :

After resizing :

I know there is a similar question on SO, but its answer does not really satisfy the need. I want to avoid overriding layoutSubviews() method as I don't think its a good enough solution.

I tried below code to unhide cell's contentView once they are displayed. But it does not work.

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
   // At the begining
    cell.contentView.hidden = NO;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ...
    // Before returning cell
    cell.contentView.hidden = YES;
    return cell;
}

Is there a way to delay displaying cell until they are resized to fit their contents?

UPDATE :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)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;
}

解决方案

I added below method in custom UITableViewCell's class.

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

I also found that returning exactly right height of cell in

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

method is crucial.

Hint :

  1. If height of any view is greater than expected then possibly calculated height of cell is greater than what is actually required.
  2. If any of views is shrinking vertically or not displaying whole content then possibly calculated height of cell is lesser than what is actually required.

Yoy can test if height is wrong by adding/removing constant value to height (variable) you calculate for cell.

这篇关于iOS的自动布局:显示其内容被更新后才细胞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆