我有一个启用了多项选择的UITableView
。我的一些细胞不会被选中。对于我实施的人:
在tableView:cellForRowAtIndexPath:
我设置cell.selectionStyle = UITableViewCellSelectionStyleNone;
在tableView:didSelectRowAtIndexPath:
我打电话给[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
。
这很好用。我唯一需要注意的是,细胞左侧的小圆圈仍然出现。当用户点击单元格时不会检查它,但是当单元格“无法选择”时,我希望不显示它。
如何隐藏某些单元格的圆圈?
谢谢
您必须在表视图数据源中实现tableView:canEditRowAtIndexPath:
方法。它可以让你阻止你想要编辑的单元格,从而不会显示圆圈。
请注意,设置cell.selectionStyle = UITableViewCellSelectionStyleNone;
不会阻止选择单元格。它只是删除了所选单元格上的任何视觉线索。
试试这个...
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if([[tableView indexPathsForSelectedRows] containsObject:indexPath]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
对UITableViewCell
进行子类化并添加以下方法对我有用:
override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(false, animated: true);
}