2013-08-28 56 views
3

我有一個UITableView並想要爲將再次出現的行設置動畫。我也想在動畫之間切換,有些單元應該得到UITableViewRowAnimationLeft和其他的UITableViewRowAnimationRight。但我不知道如何使用我的UITableViewController來實現此功能。我試過的代碼下面幾行插入cellForRowAtIndexPath滾動時在UITableViewCells中滑動

[self.tableView beginUpdates]; 
NSArray *updatePath = [NSArray arrayWithObject:indexPath]; 
[self.tableView reloadRowsAtIndexPaths:updatePath 
         withRowAnimation:UITableViewRowAnimationLeft]; 
[self.tableView endUpdates]; 

在小區滑動相反的,在小區的順序變化或它們中的一些出現了兩次。我也嘗試在創建單元格後插入這些行。

if (cell == nil) { 
... 
} else { 
    [self.tableView beginUpdates]; 
    NSArray *updatePath = [NSArray arrayWithObject:indexPath]; 
    [self.tableView reloadRowsAtIndexPaths:updatePath 
          withRowAnimation:UITableViewRowAnimationLeft]; 
    [self.tableView endUpdates]; 

回答

8

我不認爲你會成功重新加載行,一旦表開始在屏幕上顯示單元格的過程。 reloadRowsAtIndexPath通常會導致cellForRowAtIndexPath被調用,所以我很驚訝你沒有進入無限循環。相反,看起來,桌子正在進入一個糟糕的狀態。

我的建議是在這種情況下做自己的動畫,在willDisplayCell中操縱單元格的變形屬性。你可以做這樣的:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (<should animate cell>) { 
     CGFloat direction = <animate from right> ? 1 : -1; 
     cell.transform = CGAffineTransformMakeTranslation(cell.bounds.size.width * direction, 0); 
     [UIView animateWithDuration:0.25 animations:^{ 
      cell.transform = CGAffineTransformIdentity; 
     }]; 
    } 
} 

你需要提供邏輯「爲動畫單元」 - 你可能不希望動畫初始加載細胞。

+0

Thx提前。我會嘗試你的解決方案。當初始化第一行時,我認爲實現此功能並不壞。但是如果我這樣做,它會在初始加載時出現問題嗎? –

+0

這隻取決於你希望它看起來如何。 –

+0

這是一個聊天,行(帶有消息)應該從左側或右側進行動畫,具體取決於誰寫的消息。 –