gpt4 book ai didi

ios - 在 heightForRowAtIndexPath 中推断行高

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:02:11 25 4
gpt4 key购买 nike

我想做什么:

UITableViewController 方法 heightForRowAtIndexPath 中,我想推断行高的值而不是对其进行硬编码

我之前在 Interface builder 的 Storyboard 中设置了值。

错误的做法:

以下方法是错误的方法,因为它在创建实际单元格之前被调用,因此它会循环:

// wrong approach
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//AppTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AppCel" forIndexPath:indexPath];
//int height = cell.frame.size.height;
// TODO: Make it more dynamic
return 112;
}

我想到的方法:

有没有办法加载单元格 ma​​in.storyboard 文档并推断高度值?或者还有其他方法吗?

相关问题搜索:

我找到了以下 unanswered related question追溯到 2010 年。This other问题没有多大用处,因为它从单元格内容而不是 Storyboard文档中得出高度。

最佳答案

您编写的方法实际上并非完全错误。错误在于使用 dequeueReusableCellWithIdentifier:forIndexPath。相反,您想使用更简单的版本 dequeueReusableCellWithIdentifier,您可以使用它来提供原型(prototype)单元格,您可以配置、布局然后计算高度。

如果纯粹使用 iOS8,所有这些都可以通过新的自动布局为您完成。如果您也想支持 iOS7,则需要手动执行此操作。

以下链接解释了您需要使用的过程。我使用它,当你想同时支持 iOS7 和 iOS8 时,它工作得很好。

Using Auto Layout in UITableView for dynamic cell layouts & variable row heights

您还必须注意您是否在单元格中使用了基于大小类的约束,因为您必须解决一个不太明显的问题才能获得正确的高度。参见 Offscreen UITableViewCells (for size calculations) not respecting size class?

关于ios - 在 heightForRowAtIndexPath 中推断行高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28540011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com