Created
September 19, 2014 09:08
-
-
Save leejunkit/bc62016b10c580da84e9 to your computer and use it in GitHub Desktop.
Use Autolayout to dynamically size UITableViewCells
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { | |
return [self heightForBasicCellAtIndexPath:indexPath]; | |
} | |
- (CGFloat)heightForBasicCellAtIndexPath:(NSIndexPath *)indexPath { | |
static RWBasicCell *sizingCell = nil; | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
sizingCell = [self.tableView dequeueReusableCellWithIdentifier:RWBasicCellIdentifier]; | |
}); | |
[self configureBasicCell:sizingCell atIndexPath:indexPath]; | |
return [self calculateHeightForConfiguredSizingCell:sizingCell]; | |
} | |
- (CGFloat)calculateHeightForConfiguredSizingCell:(UITableViewCell *)sizingCell { | |
[sizingCell setNeedsLayout]; | |
[sizingCell layoutIfNeeded]; | |
CGSize size = [sizingCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; | |
return size.height; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment