I am using collectionview in UITableViewCell and I need to change height of collectionview to show all content. As a result, I monitor content size of collectionview, update that height and reload cell.
[self.cvTag addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
float newHeight = self.cvTag.collectionViewLayout.collectionViewContentSize.height;
if (self.verticalConstraintTag.constant != newHeight) {
self.verticalConstraintTag.constant = newHeight;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
dispatch_async(dispatch_get_main_queue(), ^{
if (self.reloadBlock)
self.reloadBlock();
self.reloadBlock = nil;
});
});
}
}
This is where I set my data in cell
- (void)setData:(Attachment *)attachment andReload:(void (^)(void))reloadAction {
self.reloadBlock = reloadAction;
[super setData:attachment];
[self setUpTag];
}
This is how I create cell.
DocumentDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell)
cell = [DocumentDetailCell loadFromNib];
[cell setW:CGRectGetWidth(self.view.frame)];
DocumentsCollection *dc = self.documents[indexPath.section - 1];
[cell setTag:indexPath.row];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell setData:dc.attachments[indexPath.row] andReload:^{
[tableView reloadData];
}];
[cell hideOrUnhideSeparator:(indexPath.row == dc.attachments.count - 1)];
return cell;
The problem is that after I reload, that content size of collection view change again! As a result, it become a loop and it never end. I have tried with reloadRow and beginUpdate/EndUpdate. It is not quite okay too. How shall I do?