当你点击UITableView中的一行时,该行会高亮显示并被选中。是否有可能禁用这个,所以点击一行什么都不做?
当前回答
你也可以将背景颜色设置为Clear,以达到与UITableViewCellSelectionStyleNone相同的效果,以防你不想/不能使用UITableViewCellSelectionStyleNone。
您将使用如下代码:
UIView *backgroundColorView = [[UIView alloc] init];
backgroundColorView.backgroundColor = [UIColor clearColor];
backgroundColorView.layer.masksToBounds = YES;
[cell setSelectedBackgroundView: backgroundColorView];
这可能会降低您的性能,因为您添加了一个额外的彩色视图到每个单元格。
其他回答
我们可以编写这样的代码
cell.selectionStyle = UITableViewCellSelectionStyleNone;
但当我们有自定义单元格xib时,在该行上面给出警告
自定义单元格xib
我们需要从界面构建器中设置选择样式为None
非常简单。在返回tableview单元格之前,使用表格视图单元格的style属性。
只需在返回表视图单元格之前编写这行代码 细胞。selectionStyle = .none
从ios6.0开始,UITableViewDelegate有tableView:shouldHighlightRowAtIndexPath:。(请参阅iOS文档。)
此方法允许您将特定行标记为不可突出显示(并且隐式地不可选择),而无需更改单元格的选择样式,也无需使用userInteractionEnabled = NO或这里记录的任何其他技术来打乱单元格的事件处理。
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
我已经看过了所有的答案,在我的用例中起作用的是:
tableView.allowSelection = false
public func tableView(_ tableView: UITableView, canFocusRowAt indexPath: IndexPath) -> Bool {
true
}
通过这种方式,表格保持可聚焦,用户可以滚动它的元素,但不能“按下/选择”它们。
简单地设置单元格。selectionStyle = .none将允许列表元素是可选择的(只是不留下灰色选择标记)。只是设置allowSelection = false会导致我的表无法聚焦。用户将无法滚动元素。
推荐文章
- iPhone上UIView和UILabels的渐变
- keychain上的分发证书中缺少私钥
- 在实现API时,我如何避免在块中捕获自我?
- 如何创建一个Swift Date对象?
- Xcode 4在目标设备上说“finished running <my app>”——什么都没有发生
- 从另一个应用程序打开设置应用程序
- 快速提取正则表达式匹配
- 如何应用梯度的背景视图的iOS Swift应用程序
- 图书馆吗?静态的?动态吗?或框架?另一个项目中的项目
- 如何用SwiftUI调整图像大小?
- Xcode 6 gitignore文件应该包括什么?
- 如何在iPhone/iOS上删除电话号码的蓝色样式?
- 检测视网膜显示
- 如何在UIImageView中动画图像的变化?
- 如何从iPhone访问SOAP服务