Skip to content

Instantly share code, notes, and snippets.

@zoejessica
Created May 13, 2014 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zoejessica/2df4bc074691b8cf2f14 to your computer and use it in GitHub Desktop.
Save zoejessica/2df4bc074691b8cf2f14 to your computer and use it in GitHub Desktop.
Calculating cell heights from auto layout prototype cells, each type of which will always be the same height
// http://stackoverflow.com/a/16881312/2199136
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellIdentifier = [self cellIdentifierForIndexPath:indexPath];
static NSMutableDictionary *heightCache;
if (!heightCache)
heightCache = [[NSMutableDictionary alloc] init];
NSNumber *cachedHeight = heightCache[cellIdentifier];
if (cachedHeight)
return cachedHeight.floatValue;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
CGFloat height = cell.bounds.size.height;
heightCache[cellIdentifier] = @(height);
return height;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment