2012-10-27 79 views
4

我有一個tableview用來自對象數組的數據填充。對象具有的價格,名稱,等等等等刪除動畫完成後更新TableView

當細胞被刪除的數據源(陣列)被更新,並且該行使用UITableViewRowAnimationFade滑落屏幕。

當一個項目被刪除某些性能,如價格,陣列中的對象可以改變,所以我因此需要更新所有單元的屏幕上作爲其數據可能已經改變。

我看過了的文檔,發現reloadRowsAtIndexPaths:withRowAnimation,我可以用indexPathsforVisibleRows結合起來,重新加載屏幕上的行,但內的tableView這樣做:commitEditingStyle:forRowAtIndexPath看起來真的很討厭,因爲它試圖執行刪除動畫,同時還重裝...

是否有等待刪除動畫執行任務之前完成的方法嗎?

這裏是我的ViewController

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) 
    { 
     // update the datasource 
     [self.dataController deleteItemAtIndex:indexPath.row]; 

     // update the table 
     [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

     // reload the table to show any changes to datasource from above deletion 
     [self.tableView reloadRowsAtIndexPaths:[self.tableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationNone]; 
    } 
} 

ASD

回答

1

編輯代碼:

好下一次嘗試。 :)這應該肯定的工作,但它需要相當多的努力......

您將需要準確跟蹤您的數據源(新增,刪除,更新)的變化,並呼籲TableVC適當的方法。

數據源源需要提供以下的委託方法:

- (void)dataControllerWillUpdateData; 
- (void)dataControllerDidRemoveObjectAtIndexPath:(NSIndexPath *)indexPath; 
- (void)dataControllerDidAddObjectAtIndexPath:(NSIndexPath *)indexPath; 
- (void)dataControllerDidUpdateObjectAtIndexPath:(NSIndexPath *)indexPath; 
- (void)dataControllerDidUpdateData; 

然後你改變tableVC的實現是這樣的:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) 
    { 
    // update the datasource 
    [self.dataController deleteItemAtIndex:indexPath.row]; 
    } 
} 


- (void)dataControllerWillUpdateData 
{ 
    [tableView beginUpdates]; 
} 

- (void)dataControllerDidRemoveObjectAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
} 

- (void)dataControllerDidAddObjectAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
} 

- (void)dataControllerDidUpdateObjectAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath]; 
} 

- (void)dataControllerDidUpdateData 
{ 
    [tableView endUpdates]; 
} 

因此,如果用戶刪除單元格,你的數據源有要確定哪些其他對象受到影響,請創建一個更改列表(請注意計算正確的indexPaths),請撥打dataControllerWillUpdateData,爲每個更改的對象調用上述適當的方法,最後再調用dataControllerDidUpdateData

當然,你也可以考慮在項目中使用CoreData。這可能需要一些工作來設置所有的東西,但是結果你會得到所有上面提到的和更多'免費'的。就個人而言,我傾向於將它用於幾乎每個包含動態tableView的項目。它有很多好處,大部分時間都值得付出。

+0

抱歉,但並沒有真正幫助,因爲模型仍然會通知控制器,同時刪除動畫正在採取導致同樣的事情發生的地方。 –

+0

刪除是用戶交互還是代碼操作的結果? – Tobi

+0

如果這是用戶操作的結果(用戶點擊「編輯」按鈕,刪除一些單元格並離開編輯模式),則可以覆蓋tableVC的setEditing:animated :.當用戶正在編輯時,您會跟蹤他所做的更改,並且當用戶將編輯模式設置爲NO(即他再次點擊編輯按鈕)時,您更新數據源 – Tobi