我想根据该行中的单元格调整 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/