2013-06-11 36 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]; 
}