当你点击UITableView中的一行时,该行会高亮显示并被选中。是否有可能禁用这个,所以点击一行什么都不做?
当前回答
我已经看过了所有的答案,在我的用例中起作用的是:
tableView.allowSelection = false
public func tableView(_ tableView: UITableView, canFocusRowAt indexPath: IndexPath) -> Bool {
true
}
通过这种方式,表格保持可聚焦,用户可以滚动它的元素,但不能“按下/选择”它们。
简单地设置单元格。selectionStyle = .none将允许列表元素是可选择的(只是不留下灰色选择标记)。只是设置allowSelection = false会导致我的表无法聚焦。用户将无法滚动元素。
其他回答
因为我最近读了这篇文章,它对我有帮助,我想发布另一个答案来巩固所有的答案(为子孙后代)。
所以,根据你想要的逻辑和/或结果,实际上有5个不同的答案:
1.禁用蓝色高亮,而不改变单元格的任何其他交互作用:
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
我使用这个当我有一个UIButton -或其他一些控件(s) -托管在UITableViewCell,我希望用户能够与控件交互,而不是单元格本身。
注意:正如Tony Million上面提到的,这并不阻止tableView:didSelectRowAtIndexPath:。我通过简单的“if”语句来解决这个问题,最常见的是测试部分并避免对特定部分执行操作。
我想到的另一种测试敲击单元格的方法是:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// A case was selected, so push into the CaseDetailViewController
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.selectionStyle != UITableViewCellSelectionStyleNone) {
// Handle tap code here
}
}
2.要对整个表执行此操作,您可以将上述解决方案应用到表中的每个单元格,但您也可以这样做:
[tableView setAllowsSelection:NO];
在我的测试中,这仍然允许UITableViewCell内的控件是交互式的。 3.要使单元格为“只读”,你可以简单地这样做:
[cell setUserInteractionEnabled:NO];
4.将整个表设置为“只读”
[tableView setUserInteractionEnabled:NO];
5.要立即确定是否突出显示一个单元格(根据这个答案隐式地包括选择),你可以实现以下UITableViewDelegate协议方法:
- (BOOL)tableView:(UITableView *)tableView
shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
场景- 1
如果你不想在表视图中选择某些特定的单元格,你可以在cellForRow函数中为这些单元格设置选择样式。
objective - c
cell.selectionStyle = UITableViewCellSelectionStyleNone;
斯威夫特4.2
cell.selectionStyle = .none
场景- 2
禁用整个表视图的选择:
objective - c
self.tableView.allowsSelection = false;
斯威夫特4.2
self.tableView.allowsSelection = false
根据我自己的实践经验,总结一下我认为的正确答案:
如果你想禁用部分单元格的选择,请使用:
cell.userInteractionEnabled = NO;
除了阻止选择,这也会阻止设置了tableView:didSelectRowAtIndexPath的单元格被调用。(感谢Tony Million的回答,谢谢!)
如果你的单元格中有需要点击的按钮,你需要改为:
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
并且你还需要忽略单元格中的任何点击- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath。
如果你想禁用整个表的选择,使用:
tableView.allowsSelection = NO;
(感谢保罗·德·巴罗斯!)
最好的解决方案是使选择样式为None
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
但是,这里我们考虑的事实是,所选状态没有使用自定义图像。
objective - c:
Below snippet disable highlighting but it also disable the call to didSelectRowAtIndexPath. So if you are not implementing didSelectRowAtIndexPath then use below method. This should be added when you are creating the table. This will work on buttons and UITextField inside the cell though. self.tableView.allowsSelection = NO; Below snippet disable highlighting and it doesn't disable the call to didSelectRowAtIndexPath. Set the selection style of cell to None in cellForRowAtIndexPath cell.selectionStyle = UITableViewCellSelectionStyleNone; Below snippet disable everything on the cell. This will disable the interaction to buttons, textfields: self.tableView.userInteractionEnabled = false;
迅速:
以下是上述Objective-C解决方案的Swift等效版本:
更换第一个溶液 self.tableView.allowsSelection = false 更换第二溶液 细胞?。selectionStyle = UITableViewCellSelectionStyle。没有一个 更换第三种溶液 self.tableView.userInteractionEnabled = false
推荐文章
- 以编程方式创建segue
- 如何使用Xcode创建。ipa文件?
- 动态改变UILabel的字体大小
- 新的自动引用计数机制是如何工作的?
- 在iPhone上确定用户是否启用了推送通知
- 是否有可能禁用浮动头在UITableView与UITableViewStylePlain?
- 错误ITMS-9000:“冗余二进制文件上传。火车1.0版本已经有一个二进制版本上传。
- Swift -转换为绝对值
- 从父iOS访问容器视图控制器
- 自定义dealloc和ARC (Objective-C)
- 调整UITableView的大小以适应内容
- 在代码中为UIButton设置一个图像
- NSRange从Swift Range?
- UICollectionView中的单元格间距
- 我如何在我的iOS应用程序中每n分钟得到一个后台位置更新?