当你点击UITableView中的一行时,该行会高亮显示并被选中。是否有可能禁用这个,所以点击一行什么都不做?
当前回答
在UITableViewDataSource协议中,在方法cellForRowAt中添加:
let cell = tableView.dequeueReusableCell(withIdentifier: "YOUR_CELL_IDENTIFIER", for: indexPath)
cell.selectionStyle = .none
return cell
OR
你可以到故事板>选择单元格>身份检查器>选择,从下拉菜单中选择none。
其他回答
你也可以从故事板中完成。单击表视图单元格,在表视图单元格下的属性检查器中,将Selection旁边的下拉菜单更改为None。
你可以使用:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UITableView的row at index path方法。
你还可以用:
[tableView deselectRowAtIndexPath:indexPath animated:NO];
在tableview中didselectrowatindexpath方法。
我也一直在与这个相当丰富的斗争,在我的UITableViewCell中有一个控件禁止使用userInteractionEnabled属性。我有一个3单元静态表的设置,2与日期,1与开/关开关。在Storyboard/IB中,我成功地使底部不可选,但当你点击它时,从顶部行之一的选择消失了。这是我设置UITableView的WIP图像:
如果你点击第三行什么都没有发生,选择将停留在第二行。该功能实际上是苹果日历应用程序添加事件时间选择屏幕的副本。
代码是令人惊讶的兼容,一直到IOS2 =/:
- (NSIndexPath *)tableView: (UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 2) {
return nil;
}
return indexPath;
}
这与设置选择样式为none一起工作,因此单元格不会在触摸事件时闪烁
在UITableViewDelegate协议中,你可以使用willSelectRowAtIndexPath方法 如果你不希望行被选中,返回nil。
同样的,你可以使用willDeselectRowAtIndexPath方法并返回nil,如果你不想让行取消选择。
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
推荐文章
- iPhone上UIView和UILabels的渐变
- keychain上的分发证书中缺少私钥
- 在实现API时,我如何避免在块中捕获自我?
- 如何创建一个Swift Date对象?
- Xcode 4在目标设备上说“finished running <my app>”——什么都没有发生
- 从另一个应用程序打开设置应用程序
- 快速提取正则表达式匹配
- 如何应用梯度的背景视图的iOS Swift应用程序
- 图书馆吗?静态的?动态吗?或框架?另一个项目中的项目
- 如何用SwiftUI调整图像大小?
- Xcode 6 gitignore文件应该包括什么?
- 如何在iPhone/iOS上删除电话号码的蓝色样式?
- 检测视网膜显示
- 如何在UIImageView中动画图像的变化?
- 如何从iPhone访问SOAP服务