当您点击 a 中的一行时UITableView
,该行将突出显示并被选中。是否可以禁用此功能,因此点击一行什么也不做?
10 回答
cell.selectionStyle = UITableViewCellSelectionStyleNone;
or [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
Further, make sure you either don't implement -tableView:didSelectRowAtIndexPath:
in your table view delegate
or explicitly exclude the cells you want to have no action if you do implement it.
实现tableView:willSelectRowAtIndexPath:
委托方法UITableView
并返回nil
。从此方法返回 nil 告诉表视图不选择行,因此tableView:didSelectRowAtIndexPath:
不会调用该方法。但是这样做并不能阻止突出显示该行。要禁用对行设置单元格选择样式的突出显示效果UITableViewCellSelectionStyleNone
为在上述答案中进行了解释。
只需设置:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
如果您想停止突出显示,请使用代码说:您想要的值为 19
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexpath.row == '19')
cell.selectionStyle = UITableViewCellSelectionStyleNone;
else
cell.selectionStyle = UITableViewCellSelectionStyleGray;
}
但是,如果你想控制单元格的选择
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexpath.row == '19')
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
对于斯威夫特 3:
cell.selectionStyle = UITableViewCellSelectionStyle.none
这是针对您有自定义单元格的情况,但我在寻找答案时发现了这个问题,所以我想我会把它留在这里以防这对其他人有帮助。
转到您的自定义单元格,并在方法中设置selectionStyle
为:.none
awakeFromNib
override func awakeFromNib() {
super.awakeFromNib()
// disable selection
selectionStyle = .none;
}
如果您在多个UITableView
s 中使用此自定义单元格,则只需在自定义单元格中设置一次,而不是每次UITableView
使用 :)
斯威夫特 4
cell.selectionStyle = UITableViewCell.SelectionStyle.none
您可以简单地为OBJ C执行此操作:
cell.selectionStyle = UITableViewCellSelectionStyleNone;