2011-08-31 61 views
1

我有一個帶編輯按鈕的UITableViewController。點擊時,用戶可以刪除或添加行到UITableView。刪除行可以正常工作。但是當我想添加一個新行時,我會從左邊看到一個空白單元格的動畫,當動畫結束時,表格視圖看起來與調用該方法之前的樣子完全相同。當我添加一個新的單元格時,我將一個新的對象添加到提供我的表格視圖的數據數組中,然後添加單元格。該數據數組得到更新。所以如果我在添加新單元格後調用[tableView reloadData],我會看到新的單元格,但沒有任何動畫。我真的很想擁有動畫。tableView的insertRowsAtIndexPath不會調用tableView的cellForRowAtIndexPath

我有一個同樣的事情的工作示例。我意識到tableView:commitEditingStyle:forRowAtIndexPath:被調用後,表視圖的數據源方法tableView:cellForRowAtIndexPath:被自動調用。在我的情況下,它沒有。我想這就是爲什麼我沒有看到新的細胞。任何想法爲什麼發生這種情況?

這是我的`的tableView:commitEditingStyle:forRowAtIndexPath:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) 
    { 
     // Delete the row from the data source 
     [self.tableData removeObjectAtIndex:[indexPath row]]; 

     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
     [defaults setObject:self.tableData forKey:self.key]; 
     [defaults synchronize]; 

     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
    else if (editingStyle == UITableViewCellEditingStyleInsert) 
    { 
     UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
     UITextField *textField = (UITextField *)[[cell contentView] viewWithTag:kTextFieldTag]; 
     NSString *textFieldText = [textField text]; 
     if (textFieldText != nil) 
     { 
      // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
      NSString *string = [[NSString alloc] initWithString:textFieldText]; 
      [self.tableData addObject:string]; 
      [string release]; 

      NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
      [defaults setObject:self.tableData forKey:self.key]; 
      [defaults synchronize]; 

      [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft]; 

      // If reloadData is being called, the new cell is displayed but without the animation 
      // [tableView reloadData]; 
     } 
     else 
     { 
      // Display alert view 
      // Code for displaying an AlertView 
     } 
    } 
} 
+0

嘗試向'super'發送一個呼叫,看看會發生什麼。您可能會無意中繞過某些父類功能。 – Hyperbole

+0

如果我調用[super tableView:tableView commitEditingStyle:editingStyle forRowAtIndexPath:indexPath],我得到一個異常。或者我誤解了你? – strave

回答

2

嘗試用[的tableView beginUpdates] [的tableView endUpdates]包裝您的插入通話。從文檔:

如果不進行插入,刪除,並選擇調用 此塊中,表屬性,如行計數可能成爲 無效。

[tableView beginUpdates]; 
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft]; 
[tableView endUpdates]; 
+1

感謝您的回答。這聽起來很有希望,但它沒有改變任何東西。 – strave

+0

您是否在endUpdates之後嘗試取消註釋reloadData調用? – jaminguy

+1

但我注意到,如果我在發佈的代碼之後執行[tableView reloadData],那麼我會得到某種動畫。動畫有點不同(可能更短),但比沒有好。 – strave

0

我有同樣的問題。從我所知道的情況來看,如果UITableView認爲被更改的單元格是可見的,它只會調用cellForRowAtIndexPath。既然你正在添加一個新的單元格,我猜這個表格不認爲它是可見的,因此不會調用這個方法。 Jaminguys解決方案適用於我,但它聽起來有點像UIKit中的錯誤。

如果您順便插入表格視圖中間的單元格,此方法可行。

相關問題