将每个单元格的计算移动到 tableView:heightForRowAtIndexPath: 的问题是,每次调用 reloadData 时都会重新计算所有单元格。太慢了,至少对于我可能有 100 行的应用程序而言。这是使用默认行高的替代解决方案,并在计算行高时缓存行高。当高度发生变化或第一次计算时,会计划重新加载表格以通知表格视图新的高度。这确实意味着行在高度变化时显示两次,但相比之下这是次要的:
@interface MyTableViewController : UITableViewController {
NSMutableDictionary *heightForRowCache;
BOOL reloadRequested;
NSInteger maxElementBottom;
NSInteger minElementTop;
}
表视图:heightForRowAtIndexPath:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// If we've calculated the height for this cell before, get it from the height cache. If
// not, return a default height. The actual size will be calculated by cellForRowAtIndexPath
// when it is called. Do not set too low a default or UITableViewController will request
// too many cells (with cellForRowAtIndexPath). Too high a value will cause reloadData to
// be called more times than needed (as more rows become visible). The best value is an
// average of real cell sizes.
NSNumber *height = [heightForRowCache objectForKey:[NSNumber numberWithInt:indexPath.row]];
if (height != nil) {
return height.floatValue;
}
return 200.0;
}
表视图:cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Get a reusable cell
UITableViewCell *currentCell = [tableView dequeueReusableCellWithIdentifier:_filter.templateName];
if (currentCell == nil) {
currentCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:_filter.templateName];
}
// Configure the cell
// +++ unlisted method sets maxElementBottom & minElementTop +++
[self configureCellElementLayout:currentCell withIndexPath:indexPath];
// Calculate the new cell height
NSNumber *newHeight = [NSNumber numberWithInt:maxElementBottom - minElementTop];
// When the height of a cell changes (or is calculated for the first time) add a
// reloadData request to the event queue. This will cause heightForRowAtIndexPath
// to be called again and inform the table of the new heights (after this refresh
// cycle is complete since it's already been called for the current one). (Calling
// reloadData directly can work, but causes a reload for each new height)
NSNumber *key = [NSNumber numberWithInt:indexPath.row];
NSNumber *oldHeight = [heightForRowCache objectForKey:key];
if (oldHeight == nil || newHeight.intValue != oldHeight.intValue) {
if (!reloadRequested) {
[self.tableView performSelector:@selector(reloadData) withObject:nil afterDelay:0];
reloadRequested = TRUE;
}
}
// Save the new height in the cache
[heightForRowCache setObject:newHeight forKey:key];
NSLog(@"cellForRow: %@ height=%@ >> %@", indexPath, oldHeight, newHeight);
return currentCell;
}