2013-06-11 38 views
1

我想設置一個UITableView來選擇選項(一次選中一個複選標記附件),就像在設置應用程序中(例如選擇Notes的字體)一樣。只允許在UITableView中一次選中一行,保留動畫

我一直在閱讀其他線程,確保我重置cellForIndexPath方法中的附件類型,並且我在didSelect...方法中做了deselectCell...。但是,我只能使用[tableView reloadData]來刷新表格。

不幸的是,取消/縮短了方法[tableView deselectRowAtIndexPath: animated:]。有沒有什麼辦法可以實現這一點,在所有行中都沒有原始循環?

回答

1

嘗試是這樣的:

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // In cellForRow... we check this variable to decide where we put the checkmark 
    self.checkmarkedRow = indexPath.row; 

    // We reload the table view and the selected row will be checkmarked 
    [tableView reloadData]; 

    // We select the row without animation to simulate that nothing happened here :) 
    [tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone]; 

    // We deselect the row with animation 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 
+0

+1的快速反應,因爲它的工作原理,似乎是完全一樣的,但是我要等待,看看是否有任何其他的解決方案。看起來很奇怪,依靠快速重新選擇行,對iOS來說非常「本機」的行爲(許多應用程序中的常見操作) – Raekye

+0

一些非常常見的行爲沒有簡單的單行解決方案:) – e1985

1

如果只允許一個對勾的時間,你可以只保留當前選擇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]; 
}