菜鸟教程小白 发表于 2022-12-12 18:24:57

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


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

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

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

<p><strong>调整大小之前:</strong> </p>

<p> <img src="/image/z79j3.png" alt="enter image description here"/> </p>

<p><strong>调整大小后:</strong> </p>

<p> <img src="/image/WqKBP.png" alt="enter image description here"/> </p>

<p>我知道有一个 <a href="https://stackoverflow.com/questions/20925247/setneedslayout-relayouts-the-cell-only-after-is-has-been-displayed" rel="noreferrer noopener nofollow">similar question</a>关于 SO,但它的答案并不能真正满足需求。我想避免覆盖 <code>layoutSubviews()</code> 方法,因为我认为它不是一个足够好的解决方案。</p>

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

<pre><code>- (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;
}
</code></pre>

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

<p>更新:</p>

<pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    chatCell *cell = (chatCell *);

      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 = [ stretchableImageWithLeftCapWidth:STRETCHED_WIDTH topCapHeight:STRETCHED_HEIGHT];

            cell.chatCellBackground.frame = ...;

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

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

    ;
    ;
    return cell;
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我在自定义 <code>UITableViewCell</code> 的类中添加了以下方法。</p>

<pre><code>- (void)layoutSubviews
{
    ;
    CGFloat maxTextWidth = .bounds.size.width * 0.40;
    self.userLabel.preferredMaxLayoutWidth = maxTextWidth;
    self.chatMessage.preferredMaxLayoutWidth = maxTextWidth;
    self.timeLabel.preferredMaxLayoutWidth = self.timeLabel.frame.size.width;
    ;
}
</code></pre>

<p>我还发现在 </p> 中返回完全正确的单元格高度

<pre><code>- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
</code></pre>

<p>方法很重要。</p>

<p><strong>提示:</strong></p>

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

<p>Yoy 可以通过向您为单元格计算的高度(变量)添加/删除常数值来测试高度是否错误。</p></p>
                                   
                                                <p style="font-size: 20px;">关于iOS 自动布局 : Display the cell only after its contents are updated,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/21457871/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/21457871/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iOS 自动布局 : Display the cell only after its contents are updated