菜鸟教程小白 发表于 2022-12-13 00:47:51

ios - 如何制作 autosize(height) UITableViewCell?


                                            <p><p>请告诉我如何让单元格根据行数自动改变高度?</p>

<p> <img src="/image/nT2pQ.png" alt="enter image description here"/> </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>在<strong>iOS8</strong>中会自动为你管理</p>

<p> <a href="http://www.shinobicontrols.com/blog/posts/2014/07/24/ios8-day-by-day-day-5-auto-sizing-table-view-cells" rel="noreferrer noopener nofollow">http://www.shinobicontrols.com/blog/posts/2014/07/24/ios8-day-by-day-day-5-auto-sizing-table-view-cells</a> </p>

<p>在 <strong>iOS7/6</strong> 中,您可以使用自动布局来执行此操作。您可以创建您的单元格设置您需要的约束。在您的情况下,您可以将标签附加到 superView 的顶部、底部、尾随和前导。</p>

<p>在 <code>- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath</code> 中可以使用 <code>; </code> 获取单元格的最小大小。例如:</p>

<pre><code>- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell *cell = self.layoutTableViewCell;

    ;

    CGSize layoutSize =;

    return layoutSize.height;
}
</code></pre>

<p>记住要避免给UILabel设置宽度和高度限制,它会自动调整大小以适应内容。</p>

<p>您可以使用以下方法在 viewDidLoad 中创建 <code>self.layoutTableViewCell</code>:</p>

<pre><code>    UINib *cellNib = ;
    self.layoutTableViewCell = [ objectAtIndex:0];
</code></pre>

<p>OT:我认为这是个人品味的问题。就我个人而言,我开始总是使用 UICollectionViews,只是为了重用更多代码并在将来需要自定义布局行为时拥有更大的灵 active 。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何制作 autosize(height) UITableViewCell?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/25306299/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/25306299/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何制作 autosize(height) UITableViewCell?