2016-06-29 48 views
0

我創建了一組類來簡化管理表視圖。他們所做的一件事就是根據更新的數據創建區段的差異,然後應用正確的插入,刪除和移動。insertRowsAtIndexPaths:不帶動畫reloadRowsAtIndexPaths:

這對我的大部分應用都很好,除了兩個不合作的表格視圖。稱他們爲表A和表B.我發現了一個解決方案,每個單獨工作,但我無法同時解決這兩個問題。我已檢查過差異是否正確生成,但出於某種原因,每個動畫都有不同的動畫。

表A和表B都只是插入行。沒有刪除或移動。

在下面的代碼BREAK_TABLE_A原因:

  • 表A - 在節= 0行的所有行> = 1(未行= 0)轉移到所述底部,同時從頂部重新動畫英寸非常難看
  • 表B - 一個漂亮的插入動畫

BREAK_TABLE_B原因:

  • 表A - 一個漂亮的插入動畫
  • 表B - 沒有動畫。就像reloadData

最終,我真的喜歡的方法BREAK_TABLE_B,因爲這可以讓我保持相同的細胞相同的數據,這意味着我可以使用configureCell這些單個細胞進行動畫:.

的代碼是相對簡單:

UITableViewRowAnimation animation = UITableViewRowAnimationFade; 
    ... 
    [self.tableView beginUpdates]; 

    NSMutableSet *insertedPaths...; 
    NSMutableSet *deletedPaths...; 
    NSMutableSet *refreshPaths...; // Will contain any cell that was present before and after the update. 

    [self.tableView moveRowsAtIndexPaths:... // Not executed in these examples 
    ... 
    [self.tableView insertRowsAtIndexPaths:insertedPaths.allObjects withRowAnimation:animation]; 
    [self.tableView deleteRowsAtIndexPaths:deletedPaths.allObjects withRowAnimation:animation]; 

    if (BREAK_TABLE_A){ 
     [self.tableView reloadRowsAtIndexPaths:refreshPaths.allObjects withRowAnimation:UITableViewRowAnimationNone]; 
    } else { //BREAK_TABLE_B - I would prefer this solution if possible 
     for (NSIndexPath *path in refreshPaths){ 
      UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:path]; 
      if (cell){ 
       NSUInteger index = [newSection.rows indexOfObjectPassingTest:^BOOL(LQTableViewRow * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { 
        return [obj.rowIdentifier  isEqual:oldSection.rows[path.row].rowIdentifier]; 
       }]; 
       LQTableViewRow *row = newSection.rows[index]; 
       [self configureCell:cell section:newSection row:row]; 
      } 
     } 
    } 

    ... update the actual data source ... 
    [self.tableView endUpdates]; 

爲了解決這個問題,我試圖在包裹[CATransaction begin/commit/flush]上面的代碼以及[UIView begin/commitAnimations]塊。 CATransaction在使用表格B時會凍結應用程序而無任何錯誤(可能正在等待其他動畫)。UIView會導致視圖(以及應用程序中的所有其他視圖)錯誤地進行動畫製作(單元格從其最終位置的底部到頂部淡入和動畫化,而不是從桌面的頂部到底部)。

我也試圖加載與虛擬數據,使他們儘可能相似的怪異的結果相似的意見。

回答

0

嗯,事實證明,使用CATransaction時的崩潰使我找到了答案。我不小心在我的一個單元格中做了兩次[CATransaction begin],而不是提交交易。修復固定動畫。

相關問題