【发布时间】:2013-07-31 04:59:57
【问题描述】:
我想实现一个表格视图,它显示特定行的可扩展单元格,所以我创建了我的自定义表格单元格,如果将其 expandContent 设置如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSString *shouldExpand = [self.datasource objectAtIndex:indexPath.row];
if([shouldExpand isEqualToString:@"expand"]){
[cell setExpandContent:@"Expand"];
}
else{
[cell setTitle:@"a line"];
}
return cell;
}
但是,为了告诉 tableview 行高,我需要实现以下代码:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
return [cell cellHeight];
}
问题是heightForRowAtIndexPath方法会调用tableView:cellForRowAtIndexPath:的1000次,如果我的数据源包含1000条数据,时间太长了。
如何解决问题?
【问题讨论】:
-
那么就不要调用 cellForRowAtIndexPath 了,反正这样做很奇怪。改为查询您的数据源。
-
您的单元格是否只有两种尺寸?是否所有未展开的单元格都具有相同的高度,并且所有展开的行都具有相同的高度?
-
所有未展开的单元格高度相同,但并非所有展开的行高度都相同@rob mayoff
-
你让用户同时展开多行吗?
标签: ios uitableview ios5