ios - 根据该行中的单元格设置 UITableView 的行高

标签 ios objective-c

我想根据该行中的单元格调整 UITableView 的行高。

最初,我尝试使用

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

但是,问题是,当加载表格时,调用流程似乎是:

第一次调用:

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

然后调用:

(UIViewTableCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

这意味着,在生成我的单元格之前,我必须告诉表格其行的高度。

然而,我想要的恰恰相反,即在我的单元格生成后,我告诉表格这些行的高度。

有什么方法可以实现吗?

最佳答案

根据 tableView: cellForRowAtIndexPath: 中的数据为每一行创建一个高度数组,在 tableView heightForRowAtIndexPath: 中,只需从该数组中获取高度值。

在你的实现文件中声明:

NSMutableArray *heights;

viewDidLoad: 中初始化它:

heights = [NSMutableArray array];

tableView: cellForRowAtIndexPath: 中设置每一行的高度:

[heights addObject:[NSNumber numberWithFloat:HEIGHT]];

并在 tableView heightForRowAtIndexPath: 中返回所需的高度:

return [heights objectAtIndex:indexPath.row];

这应该有效,因为 tableView: cellForRowAtIndexPath: 为每个单元格调用,然后为每个单元格调用 tableView heightForRowAtIndexPath:

关于ios - 根据该行中的单元格设置 UITableView 的行高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19836354/

相关文章:

ios - IOS 上的 getifaddrs 是公共(public) API 吗?

iphone - iPhone双滑解锁?

ios - 不调用 UIWebView webViewDidStartLoad 和完成

objective-c - 调用方法时会发生什么?争论在哪里?

objective-c - 函数调用后 XCode 3.2.6 堆栈损坏

ios - 检测点击 UITableViewCell 内的 UICollectionView

ios - iTunes Artwork 图像类型删除?

ios - 使用 GCD 为 UITableView 异步下载图像

objective-c - iOS8 在 iPad Air 上使用 HealthKit 时如何修复 "Health data is unavailable on this device"

ios - 如何有效地使用 NSMutableDictionaries 作为字典的键?