2012-10-24 34 views
4

在其中一個視圖中有一個UITableView,它會經常更新。 跟蹤更改,可使用「reloadRowsAtIndexPaths」以經典的方式完成保持所選單元格重載reloadRowsAtIndexPaths UITableView

-(void)refreshCells:(NSArray *)changedCells 
{ 
    NSLog(@"refreshCells %i",[changedCells count]); 
    [TableView beginUpdates]; 
    [TableView reloadRowsAtIndexPaths:changedCells withRowAnimation:UITableViewRowAnimationBottom]; 
    [TableView endUpdates]; 
} 

問:我怎樣才能保護用戶最後選擇的小區。每次更新refreshCells後單元格的位置可能會改變?

+0

Afaik reloadRowsAtIndexPaths僅爲'changedCells'中的每個索引路徑調用delegate cellForRowAtIndexPath,因此單元格數保持不變,因此索引路徑保持不變。如果當然由於某種原因你改變了cellForRowAtIndexPath的返回順序,只有你可以知道選中的單元格的位置^^ – Tobi

+2

我也有這個問題。我正在使用'reloadRowsAtIndexPaths:withRowAnimation:'來重新加載一個單元格。爲什麼取消選擇該單元格?這似乎是iOS SDK的一個錯誤。否則,該方法應該被調用:'reloadAndDeselectRowsAtIndexPaths:withRowAnimation:'。 – ma11hew28

回答

9

您可以

NSIndexPath *selectedRow = [self.tableView indexPathForSelectedRow]; 

保存當前選擇重裝之前與

if (selectedRow) { 
    [self.tableView selectRowAtIndexPath:selectedRow animated:NO scrollPosition:UITableViewScrollPositionNone]; 
} 
重裝後

重新選擇。除非您撥打insertRowsAtIndexPaths:deleteRowsAtIndexPaths:,否則單元格位置不會更改。

+2

將動畫屬性設置爲NO不會讓用戶看到取消選擇和選擇步驟。因此,用戶在重新加載單元格時不會注意到該行被取消選擇。 – Sam

+0

@Sam:這是個好主意,謝謝。 –

+1

@Sam,我根據你的建議更新了答案。 – ma11hew28

相關問題