Skip to content

Commit b319abb

Browse files
Tyler FoxTyler Fox
authored andcommitted
Modify table view estimated row height implementation
Use the estimatedRowHeight property of the table view instead of the delegate method to further increase performance.
1 parent 8f75ea1 commit b319abb

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

TableViewCellWithAutoLayout/TableViewController/RJTableViewController.m

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,17 @@ - (id)initWithStyle:(UITableViewStyle)style
6363
- (void)viewDidLoad
6464
{
6565
[super viewDidLoad];
66-
67-
[self.tableView registerClass:[RJTableViewCell class] forCellReuseIdentifier:CellIdentifier];
6866

6967
self.title = @"Auto Layout Table View";
7068
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(clear:)];
7169
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addRow:)];
7270

71+
[self.tableView registerClass:[RJTableViewCell class] forCellReuseIdentifier:CellIdentifier];
72+
73+
// Setting the estimated row height prevents the table view from calling tableView:heightForRowAtIndexPath: for every row in the table on first load;
74+
// it will only be called as cells are about to scroll onscreen. This is a major performance optimization.
75+
self.tableView.estimatedRowHeight = UITableViewAutomaticDimension;
76+
7377
self.tableView.allowsSelection = NO;
7478
}
7579

@@ -205,8 +209,17 @@ - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPa
205209
return height;
206210
}
207211

212+
/*
208213
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
209214
{
215+
// If you are just returning a constant value from this method, you should instead just set the table view's
216+
// estimatedRowHeight property (in viewDidLoad or similar), which is even faster as the table view won't
217+
// have to call this method for every row in the table view.
218+
//
219+
// Only implement this method if you have row heights that vary by extreme amounts and you notice the scroll indicator
220+
// "jumping" as you scroll the table view when using a constant estimatedRowHeight. If you do implement this method,
221+
// be sure to do as little work as possible to get a reasonably-accurate estimate.
222+
210223
// NOTE for iOS 7.0.x ONLY, this bug has been fixed by Apple as of iOS 7.1:
211224
// A constraint exception will be thrown if the estimated row height for an inserted row is greater
212225
// than the actual height for that row. In order to work around this, we need to return the actual
@@ -218,5 +231,6 @@ - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(
218231
219232
return UITableViewAutomaticDimension;
220233
}
234+
*/
221235

222236
@end

0 commit comments

Comments
 (0)