2011-05-28 76 views
5

在我的UITableView中,当它进入编辑模式时,我只想选择一些选定的单元格。我知道UITableView班有财产allowsSelectionDuringEditing,但这适用于整个UITableView。我没有看到任何相关的委托方法在每个单元的基础上进行设置。UITableView编辑时的单元格选择模式

我能想出的最佳解决方案是将allowsSelectionDuringEditing设置为YES。然后,在didSelectRowAtIndexPath中,如果表视图正在编辑,则过滤掉任何不需要的选择。另外,在cellForRowAtIndexPath中,将这些单元格selectionStyle更改为无。

问题是进入编辑模式不会重新加载UITableViewCells,所以他们的selectionStyle直到他们滚动离屏时才会更改。所以,在setEditing中,我还必须迭代可见单元格并设置它们的selectionStyle

这有效,但我只是想知道是否有更好/更优雅的解决方案来解决这个问题。我的代码的基本轮廓附加。任何建议非常感谢!谢谢。

- (void) tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath { 

    if (self.editing && ![self _isUtilityRow:indexPath]) return; 
    // Otherwise, do the normal thing... 
} 

- (UITableViewCell*) tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath { 

    // UITableViewCell* cell = ... 

    if (self.editing && ![self _isUtilityRow:indexPath]) 
    { 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    } 
    else 
    { 
     cell.selectionStyle = UITableViewCellSelectionStyleBlue; 
    } 

    return cell; 
} 

- (void) setEditing:(BOOL)editing animated:(BOOL)animated { 

    [super setEditing:editing animated:animated]; 

    if (editing) 
    { 
     for (UITableViewCell* cell in [self.tableView visibleCells]) 
     { 
      if (![self _isUtilityRow:[self.tableView indexPathForCell:cell]]) 
      { 
       cell.selectionStyle = UITableViewCellSelectionStyleNone; 
      } 
     } 
    } 
    else 
    { 
     for (UITableViewCell* cell in [self.tableView visibleCells]) 
     { 
      if (![self _isUtilityRow:[self.tableView indexPathForCell:cell]]) 
      { 
       cell.selectionStyle = UITableViewCellSelectionStyleBlue; 
      } 
     } 
    } 
} 
+0

您可以在进入编辑模式时重新加载表格,或者在选择该表格后立即取消选择该行,而不是将选择样式设置为无... – AMayes 2013-01-10 21:29:11

回答

0

我不知道你是如何应用的工作原理,但或许你可以尝试使用下面的某处你的数据源定义:

// Individual rows can opt out of having the -editing property set for them. If not implemented, all rows are assumed to be editable. 

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath; 

进入编辑模式时,使用功能过滤第一个选择级别,然后进入第二个选择级别

+0

这应该工作。 – CW0007007 2013-10-02 13:43:32