Skip to content

Instantly share code, notes, and snippets.

@bobspryn
Last active January 4, 2016 20:59
Show Gist options
  • Select an option

  • Save bobspryn/8677612 to your computer and use it in GitHub Desktop.

Select an option

Save bobspryn/8677612 to your computer and use it in GitHub Desktop.
// in tableVC
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
JotoEventTableViewCell *sizingCell = [tableView dequeueReusableCellWithIdentifier:@"JotoEvent"];
JotoEvent *event = [self.fetchedResultsController objectAtIndexPath:indexPath];
[sizingCell updateFriendsForEvent:[event.friends allObjects]];
sizingCell.frame = CGRectMake(0, 0, self.tableView.frame.size.width, sizingCell.frame.size.height);
[sizingCell setNeedsLayout];
[sizingCell layoutIfNeeded];
CGFloat height = [sizingCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
// Add an extra point to the height to account for the cell separator, which is added between the bottom
// of the cell's contentView and the bottom of the table view cell.
height += 1.0f;
return height;
}
// in cell
- (void) updateFriendsForEvent:(NSArray *)friends {
self.friends = friends;
[self.contentView removeConstraint: self.categoriesToSuperViewBottomConstraint]];
[self.contentView removeConstraint: self.collectionViewToSuperViewBottomConstraint]];
self.collectionViewToSuperViewBottomConstraint = self.categoriesToSuperViewBottomConstraint = nil;
[self updateConstraints];
}
- (void) updateConstraints {
if(!self.categoriesToSuperViewBottomConstraint) {
self.categoriesToSuperViewBottomConstraint = [NSLayoutConstraint constraintWithItem:self.categoriesLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeBottom multiplier:1 constant:8];
self.collectionViewToSuperViewBottomConstraint = [NSLayoutConstraint constraintWithItem:self.collectionView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeBottom multiplier:1 constant:8];
if(self.friends.count == 0) {
self.categoriesToSuperViewBottomConstraint.priority = 1000;
self.collectionViewToSuperViewBottomConstraint = 999;
} else {
self.categoriesToSuperViewBottomConstraint.priority = 999;
self.collectionViewToSuperViewBottomConstraint = 1000;
}
[self.contentView addConstraints:@[self.categoriesToSuperViewBottomConstraint, self.collectionViewToSuperViewBottomConstraint]]
[super updateConstraints];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment