Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS自动布局:仅在更新单元格内容后显示单元格_Ios_Objective C_Uitableview_Autolayout - Fatal编程技术网

iOS自动布局:仅在更新单元格内容后显示单元格

iOS自动布局:仅在更新单元格内容后显示单元格,ios,objective-c,uitableview,autolayout,Ios,Objective C,Uitableview,Autolayout,我使用自动布局并显示带有自定义动态单元格的表格。该表基本上显示两人之间的聊天。因此,每个单元格的文本消息都不同 这里的问题是显示第一个单元格,然后在一秒钟内调整其内容的大小。这是显而易见的,看起来很糟糕 请参见下图,了解调整大小前后的外观 调整大小之前: 调整大小后: 我知道有一个答案是肯定的,但它的答案并不能真正满足需要。我想避免重写layoutSubviews()方法,因为我认为这不是一个足够好的解决方案 我尝试在下面的代码中取消隐藏单元格的contentView,一旦它们显示出来。但它

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

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

请参见下图,了解调整大小前后的外观

调整大小之前:

调整大小后:

我知道有一个答案是肯定的,但它的答案并不能真正满足需要。我想避免重写
layoutSubviews()
方法,因为我认为这不是一个足够好的解决方案

我尝试在下面的代码中取消隐藏单元格的
contentView
,一旦它们显示出来。但它不起作用

- (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;
}
有没有办法延迟显示单元格,直到调整大小以适应其内容

更新:

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

我在custom
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)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
方法至关重要

提示:

  • 若任何视图的高度大于预期值,则计算的单元高度可能大于实际需要的高度
  • 若任何视图垂直收缩或不显示整个内容,则计算的单元格高度可能小于实际需要的高度

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

    在哪里加载单元格内容?这应该在CellForRowatineXpath方法中完成。@ClintWarner我只在
    CellForRowatineXpath:
    方法中完成。我只是没有在问题中添加它。如果在单元格上调用layoutIfNeeded会发生什么?没有理由需要延迟显示单元格,直到子视图调整大小以适应其内容。如果一切正常,则单元格及其所有子视图都应具有其内容集,并且应在第一次在屏幕上绘制单元格及其视图层次结构之前进行布局和调整大小。在没有看到更多代码的情况下,很难帮助您调试出现问题的地方。@RaphaelOliveira我已经给它打过电话了。请检查我添加到问题中的代码。