Skip to content

Instantly share code, notes, and snippets.

@mickeyl
Last active August 20, 2016 11:25
Show Gist options
  • Save mickeyl/7abc33f65b9fed838e9f4c3e48e9d541 to your computer and use it in GitHub Desktop.
Save mickeyl/7abc33f65b9fed838e9f4c3e48e9d541 to your computer and use it in GitHub Desktop.
Work around a crash in UITableView when changing layout during scrolling
-(void)triggerRelayoutIfNecessary
{
[_activityIndicator stopAnimating];
float newHeight = [self.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
if ( newHeight != _lastHeight )
{
UITableView* tv = (UITableView*)[self LT_findFirstSuperviewOfClass:UITableView.class];
if ( tv.isDragging || tv.isDecelerating )
{
LOG( @"Can't trigger updating because tableview is being scrolled" );
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self triggerRelayoutIfNecessary];
});
return;
}
_lastHeight = newHeight;
[self setNeedsUpdateConstraints];
[tv beginUpdates];
[tv endUpdates];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment