我认为要更改“ on”为“ OFF”的复选标记,我必须更改 CellAccessoryType 之间 nonecheckmarkdidSelectRowAtIndexPath?

因为我已经这样做了,但我注意到这种行为与iPhone上自动锁定设置上的Checkmark单元格并不完全相同。

还是有其他方法可以处理的其他方式?

有帮助吗?

解决方案

  1. 将属性保存在您的视图控制器中 selectedRow, ,代表一行代表表部分中检查项目的索引。

  2. 在您的视图控制器中 -tableView:cellForRowAtIndexPath: 委托法,设置 accessoryTypecellUITableViewCellAccessoryCheckmark 如果细胞的 indexPath.row 等于 selectedRow 价值。否则,将其设置为 UITableViewCellAccessoryNone.

  3. 在您的视图控制器中 -tableView:didSelectRowAtIndexPath: 委托法,设置 selectedRow 价值 indexPath.row 选择的,例如: self.selectedRow = indexPath.row

其他提示

另一个解决方案:

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSIndexPath *oldIndex = [self.tableView indexPathForSelectedRow];
    [self.tableView cellForRowAtIndexPath:oldIndex].accessoryType = UITableViewCellAccessoryNone;
    [self.tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
    return indexPath;
}

是的:您不必检查是否是否 oldIndexnil :)


Swift 3及以后:

override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
    if let oldIndex = tableView.indexPathForSelectedRow {
        tableView.cellForRow(at: oldIndex)?.accessoryType = .none
    }
    tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark

    return indexPath
}

Zyphrax提出了一个很好的解决方案,对我有用!如果您需要清除先前的行,请使用:

[self.tableView reloadData]; 

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

它应该是

didhighlightrowatindExpath

代替

TableView:DidSelectRowatIndExpath

Alex Answer仅在添加重新加载表之后才适用于我在.h

{
  int selectedCell;
}
@property(nonatomic,readwrite)int selectedCell;

在.m *CellForrowatIndExpath*

if(indexPath.row == selectedCell)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        cell.selected = YES;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selected = NO;
    }

以及在任何didhighlightrowatindExpath或didSelectRowatIndExpath的任何地方

 self.selectedCell = indexPath.row;
    [self.tableView reloadData]; 

迅速:

var selectedCell:UITableViewCell?{
    willSet{
        selectedCell?.accessoryType = .None
        newValue?.accessoryType = .Checkmark
    }
}

然后:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    selectedCell = self.tableView.cellForRowAtIndexPath(indexPath)
    self.mapView.selectAnnotation(self.mapView.annotations[indexPath.row], animated: true)
}

如果要使用自定义映像作为辅助类型,请使用以下代码

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UIImageView *imgCheckMark = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"select_icon.png"]];
    [imgCheckMark setFrame:CGRectMake(self.view.frame.size.width - 30, 25, 14, 18)];
    imgCheckMark.tag = 1000+indexPath.row;

    NSIndexPath *oldIndex = [self.tblSelectService indexPathForSelectedRow];
    [self.tblSelectService cellForRowAtIndexPath:oldIndex].accessoryView = UITableViewCellAccessoryNone;
    [self.tblSelectService cellForRowAtIndexPath:indexPath].accessoryView = imgCheckMark;
    return indexPath;
}

对于Swift

  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
  }

  func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        tableView.cellForRow(at: indexPath)?.accessoryType = .none
  }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top