I do not want animation in the begin updates, end updates block for uitableview?

Objective CUitableview

Objective C Problem Overview


I have a UITableView that is using a custom table cell and each cell has a UIWebView.

Because UIWebView took along time to load, i want to avoid reloading them at all cost. In some situations, I have all cells loaded, but their heights are messed up. Therefore, I need to "relayout" the table without triggering the "cellForRow" function.

  1. I definitely cannot use reloadData... as it will reload the cells again.
  2. I tried tableView.setNeedDisplay, setNeedsLayout etc, none of them is able to rearrange the table cells
  3. The only way it worked, is to call beginupdates/endupdates block, this block is able to relayout my table without firing cellForRow! BUT, I didnt want the animation! This block produces an animation effect but i do not want it...

How can I solve my problem?

Objective C Solutions


Solution 1 - Objective C

[UIView setAnimationsEnabled:NO];
[tableView beginUpdates];
[tableView endUpdates];
[UIView setAnimationsEnabled:YES];

Solution 2 - Objective C

One more way to do it using blocks

Obj-C

[UIView performWithoutAnimation:^{
   [self.tableView beginUpdates];
   [self.tableView endUpdates];
}];

Swift

UIView.performWithoutAnimation {
    tableView.beginUpdates()
    tableView.endUpdates()   
}

Solution 3 - Objective C

working on my project, but not a common solution.

let loc = tableView.contentOffset
UIView.performWithoutAnimation {
            
    tableView.reloadData()
            
    tableView.layoutIfNeeded()
    tableView.beginUpdates()
    tableView.endUpdates()

    tableView.layer.removeAllAnimations()
}
tableView.setContentOffset(loc, animated: true)//animation true may perform better

Solution 4 - Objective C

Swifties I had to do the following for this to work:

// Sadly, this is not as simple as calling:
//      UIView.setAnimationsEnabled(false)
//      self.tableView.beginUpdates()
//      self.tableView.endUpdates()
//      UIView.setAnimationsEnabled(true)

// We need to disable the animations.
UIView.setAnimationsEnabled(false)
CATransaction.begin()

// And we also need to set the completion block,
CATransaction.setCompletionBlock { () -> Void in
    // of the animation.
    UIView.setAnimationsEnabled(true)
}

// Call the stuff we need to.
self.tableView.beginUpdates()
self.tableView.endUpdates()

// Commit the animation.
CATransaction.commit()

Solution 5 - Objective C

I prefer to have a smooth transition:

CGPoint offset = self.tableView.contentOffset;
[UIView transitionWithView:self.tableView duration:0.5 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
		[self.tableView reloadData];
		self.tableView.contentOffset = offset;
	} completion:nil];

give it a try.

Solution 6 - Objective C

I wanted to updated the cell height for section 5 and following code worked for me:

UiView.setAnimationsEnabled(False)
self.productTableView.reloadSections(NSIndexSet(index: SectionType.ProductDescription.hashValue), withRowAnimation: UITableViewRowAnimation.None)
self.productTableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 5), atScrollPosition: UITableViewScrollPosition.Bottom, animated: false)
UIView.setAnimationsEnabled(true)

Solution 7 - Objective C

for OBJ-C

[UIView setAnimationsEnabled:NO];
[tableView beginUpdates];
[tableView endUpdates];
[UIView setAnimationsEnabled:YES];

for swift

UIView.setAnimationsEnabled(false)
tableView.beginUpdates()
tableView.endUpdates()
UIView.setAnimationsEnabled(true)

Solution 8 - Objective C

In my case worked this:

func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
    tableView.beginUpdates()
    tableView.endUpdates()
    return true
}

func textViewDidChange(_ textView: UITextView) {
    DispatchQueue.main.async {
        self.tableView.beginUpdates()
        self.tableView.endUpdates()
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmktoView Question on Stackoverflow
Solution 1 - Objective CEvgeny ShurakovView Answer on Stackoverflow
Solution 2 - Objective CDmitriyView Answer on Stackoverflow
Solution 3 - Objective ChstdtView Answer on Stackoverflow
Solution 4 - Objective CNeil JaphthaView Answer on Stackoverflow
Solution 5 - Objective CJorge ArimanyView Answer on Stackoverflow
Solution 6 - Objective CAadi007View Answer on Stackoverflow
Solution 7 - Objective CdimaskpzView Answer on Stackoverflow
Solution 8 - Objective Cuser16658136View Answer on Stackoverflow