如何删除另一次点击的复选标记?

时间:2011-12-05 16:02:34

标签: ios uitableview

我想创建一个用户可以选中的表格,并使用复选标记取消选择:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath 
{
    ...;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
}

- (void)tableView:(UITableView *) tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    ...;
    newCell.accessoryType = UITableViewCellAccessoryCheckmark;
    ...;
}

当我再次单击带勾选标记的单元格时,我试图删除复选标记,但只需要点击2次而不是一次。

如果我将选择样式设置为默认值,当我单击所选行时,它将删除蓝色突出显示;再次单击,它会删除复选标记。

我还尝试了didSelectRowAtIndexPath中的一些条件语句,但它们也只响应了第二次点击。

导致问题的原因是什么?如何解决?

5 个答案:

答案 0 :(得分:15)

你可以尝试这个:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  NSUInteger index = [[tableView indexPathsForVisibleRows] indexOfObject:indexPath];

  if (index != NSNotFound) {
    UITableViewCell *cell = [[tableView visibleCells] objectAtIndex:index];
    if ([cell accessoryType] == UITableViewCellAccessoryNone) {
      [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    } else {
      [cell setAccessoryType:UITableViewCellAccessoryNone];
    }
  }
}

这应该在每次触摸时切换单元格的复选标记。 如果您另外只想要一次选择一个单元格,还要添加以下内容:

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUInteger index = [[tableView indexPathsForVisibleRows] indexOfObject:indexPath];
    if (index != NSNotFound) {
        UITableViewCell *cell = [[tableView visibleCells] objectAtIndex:index];
        [cell setAccessoryType:UITableViewCellAccessoryNone];
    }
}

如果您不想要蓝色的hilight背景,只需在创建单元格后将单元格的选择样式设置为UITableViewCellSelectionStyleNone

答案 1 :(得分:1)

基于Starter与Apple文档的链接(我在Swift 3中的代码):

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)
    if let cell = tableView.cellForRow(at: selectedIndexPath) {
        cell.accessoryType = .none
    }
    if let cell = tableView.cellForRow(at: indexPath) {
        cell.accessoryType = .checkmark
    }
    selectedIndexPath = indexPath
}

重点是跟踪当前选定的单元格并相应地更改cell.accessoryType。另外,不要忘记在tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath)中根据selectedIndexPath正确设置cell.accessoryType。

答案 2 :(得分:0)

查看this Apple官方文档

最好的解决方案

答案 3 :(得分:0)

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
       if (index != NSNotFound) {
              UITableViewCell *cell = [[tableView visibleCells] objectAtIndex:index];
              if (cell.accessoryType == UITableViewCellAccessoryNone)
                 cell.accessoryType = UITableViewCellAccessoryCheckmark;
             else
                 cell.accessoryType = UITableViewCellAccessoryNone;
             [self.tableName reloadData];
        }
}

这有助于切换复选标记(再次单击带勾选标记的单元格时删除复选标记)

答案 4 :(得分:0)

您可以使用此:

您可以使用此:

如果tableView.cellForRow(at:indexPath)?. accessoryType == .checkmark {tableView.cellForRow(at:indexPath)?. accessoryType = .none} else {tableView.cellForRow(at:indexPath)?. accessoryType =。选中标记} tableView.deselectRow(at:indexPath,animation:true)