2012-02-16 26 views
65

我有一个UITableView。 它使用自定义表格单元格,每个单元格都有一个uiwebview。 Bcoz UIWebView花费时间加载,我想避免不惜一切代价重新加载它们。 在某些情况下,我已加载所有单元格,但其高度已混乱。 因此,我需要“重新布局”表格而不触发“cellForRow”功能。我不想在开始更新动画,结束更新块为uitableview?

  1. 我绝对不能使用reloadData ...因为它会重新加载单元格。
  2. 我试过tableView.setNeedDisplay,setNeedsLayout等,他们都不是能够重新排列表格单元格
  3. 只是它的工作方式,是调用beginupdates/endupdates块,该块能够重新布局我的表兵不血刃cellForRow !但是,我不想动画!此块产生动画效果,但我不想要它...

我该如何解决我的问题?

回答

182
[UIView setAnimationsEnabled:NO]; 
[tableView beginUpdates]; 
[tableView endUpdates]; 
[UIView setAnimationsEnabled:YES]; 
+1

这是一个聪明,聪明的把戏。我会将其添加到我的工具箱中! – 2015-02-06 22:57:03

+1

这太棒了!我发现它有一些最有趣的应用程序。我发现即使你传入'''UITableViewRowAnimationNone''',reload部分的方法总是会动画的,但是动画可以很容易的绕过! – 2015-04-21 15:11:42

+1

Woow!你是老板,老兄! – debiasej 2015-10-30 11:17:12

2

Swifties 我不得不做以下这个工作:

// 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() 
+0

帮助了我。谢谢。重要的是在完成块中放置'setAnimationsEnabled(true)'。 – 2016-05-25 15:02:39

+0

但是,这不会更新页脚,直到你滚动tableview。 – 2017-11-30 05:46:04

+0

@Bean先生,这是什么情况? (即你的桌面视图总是有一个页脚,你想插入一个或删除一个?) – NeilJaff 2017-12-04 11:25:02

37

使用块

对象 -

[UIView performWithoutAnimation:^{ 
    [self.tableView beginUpdates]; 
    [self.tableView endUpdates]; 
}]; 
另一种方式

斯威夫特

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

我想更新单元格高度为第5和下面的代码为我工作:

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) 
1

我希望有一个平稳过渡:

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

给予它是一个尝试。