【问题标题】:I do not want animation in the begin updates, end updates block for uitableview?我不想在开始更新、结束更新块中为 uitableview 设置动画?
【发布时间】:2012-03-07 18:30:46
【问题描述】:

我有一个使用自定义表格单元格的 UITableView,每个单元格都有一个 UIWebView。

因为 UIWebView 需要花费时间来加载,所以我想不惜一切代价避免重新加载它们。 在某些情况下,我已加载所有单元格,但它们的高度搞砸了。 因此,我需要在不触发“cellForRow”函数的情况下“重新布局”表格。

  1. 我绝对不能使用 reloadData... 因为它会再次重新加载单元格。
  2. 我尝试了 tableView.setNeedDisplay、setNeedsLayout 等,它们都无法重新排列表格单元格
  3. 唯一可行的方法是调用 beginupdates/endupdates 块,该块能够重新布局我的表而无需触发 cellForRow!但是,我不想要动画!这个块会产生动画效果,但我不想要它......

我该如何解决我的问题?

【问题讨论】:

    标签: objective-c uitableview


    【解决方案1】:

    对于 OBJ-C

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

    快速

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

    【讨论】:

      【解决方案2】:

      在我的情况下是这样的:

      func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
          tableView.beginUpdates()
          tableView.endUpdates()
          return true
      }
      
      func textViewDidChange(_ textView: UITextView) {
          DispatchQueue.main.async {
              self.tableView.beginUpdates()
              self.tableView.endUpdates()
          }
      }
      

      【讨论】:

        【解决方案3】:

        在我的项目上工作,但不是一个通用的解决方案。

        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
        

        【讨论】:

          【解决方案4】:

          我更喜欢平稳过渡:

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

          试一试。

          【讨论】:

            【解决方案5】:

            另一种使用块的方法

            Obj-C

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

            斯威夫特

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

            【讨论】:

              【解决方案6】:

              我想更新第 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)
              

              【讨论】:

                【解决方案7】:

                雨燕 为此,我必须执行以下操作:

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

                【讨论】:

                • 帮助了我。谢谢。将setAnimationsEnabled(true) 放在完成块中很重要。
                • 但这不会更新页脚,直到您滚动表格视图。
                • @Mr.Bean 完整的案例是什么? (即您的 tableview 是否总是有一个页脚,您是要插入一个还是删除一个?)
                • 总是有一个Footer,只需要更新footer(即footer里面有2个view,需要根据情况隐藏/取消隐藏view)
                • @Mr.Bean 您的更新块包含什么?这里最可扩展的方法是重新加载该部分。
                【解决方案8】:
                [UIView setAnimationsEnabled:NO];
                [tableView beginUpdates];
                [tableView endUpdates];
                [UIView setAnimationsEnabled:YES];
                

                【讨论】:

                • 这太棒了!我发现它有一些最有趣的应用。我发现即使你传入UITableViewRowAnimationNone,reload section 方法也会始终动画,但是这样可以轻松绕过动画!
                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2014-11-04
                • 1970-01-01
                • 2016-01-03
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多