如果只允許一個對勾的時間,你可以只保留當前選擇indexPath(或適當的替代指標)的屬性,然後你只需要更新兩排。
否則,你將不得不循環。通常情況下,我有一個configureCell:atIndexPath:
的方法,我可以在任何地方(包括cellForRowAtIndexPath
)調用與reloadVisibleCells
方法相結合:
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
//cell configuration logic
}
- (void)reconfigureVisibleCells
{
for (UITableViewCell *cell in [self.tableView visibleCells]) {
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
[self configureCell:cell atIndexPath:indexPath];
}
}
或者,如果你想擁有你可以用重裝細胞的更傳統的方法在begin/endUpdates
三明治內置的行動畫:
- (void)reloadVisibleCells
{
[self.tableView beginUpdates];
NSMutableArray *indexPaths = [NSMutableArray array];
for (UITableViewCell *cell in [self.tableView visibleCells]) {
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
[indexPaths addObject:indexPath];
}
[self.tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
}
+1的快速反應,因爲它的工作原理,似乎是完全一樣的,但是我要等待,看看是否有任何其他的解決方案。看起來很奇怪,依靠快速重新選擇行,對iOS來說非常「本機」的行爲(許多應用程序中的常見操作) – Raekye
一些非常常見的行爲沒有簡單的單行解決方案:) – e1985