52

我是否正确地认为要将“on”的复选标记更改为“off”,我必须更改CellAccessoryTypebetweennonecheckmarkon didSelectRowAtIndexPath

因为我已经这样做了,但我注意到该行为与 iphone 上自动锁定设置上的复选标记单元格并不完全相同。

还是有其他方法可以处理复选标记?

4

8 回答 8

81
  1. 在视图控制器中保留一个名为 的属性selectedRow,它表示代表表格部分中选中项的行的索引。

  2. 在您的视图控制器的-tableView:cellForRowAtIndexPath:委托方法中,如果单元格的值等于该值,则将 的 设置accessoryType为。否则,将其设置为。cellUITableViewCellAccessoryCheckmarkindexPath.rowselectedRowUITableViewCellAccessoryNone

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

于 2010-05-09T09:51:47.843 回答
61

另一种解决方案:

-(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
}
于 2013-04-06T08:38:57.450 回答
5

迅速:

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)
}
于 2016-01-30T23:01:12.537 回答
4

Zyphrax 提出了一个非常适合我的解决方案!如果您需要清除先前选择的行,只需使用:

[self.tableView reloadData]; 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
于 2013-08-09T16:42:43.047 回答
3

它应该是

didHighlightRowAtIndexPath

代替

表视图:didSelectRowAtIndexPath

于 2012-11-30T13:17:20.657 回答
3

亚历克斯回答仅在.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]; 
于 2013-02-25T09:45:31.123 回答
3

为了迅速

  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
  }
于 2019-01-18T21:40:30.083 回答
0

如果您想使用自定义图像作为附件类型,请使用以下代码

-(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;
}
于 2015-06-25T05:57:13.150 回答