当你点击UITableView中的一行时,该行会高亮显示并被选中。是否有可能禁用这个,所以点击一行什么都不做?
当前回答
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
其他回答
根据我自己的实践经验,总结一下我认为的正确答案:
如果你想禁用部分单元格的选择,请使用:
cell.userInteractionEnabled = NO;
除了阻止选择,这也会阻止设置了tableView:didSelectRowAtIndexPath的单元格被调用。(感谢Tony Million的回答,谢谢!)
如果你的单元格中有需要点击的按钮,你需要改为:
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
并且你还需要忽略单元格中的任何点击- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath。
如果你想禁用整个表的选择,使用:
tableView.allowsSelection = NO;
(感谢保罗·德·巴罗斯!)
最好的解决方案是使选择样式为None
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
但是,这里我们考虑的事实是,所选状态没有使用自定义图像。
快速解决方案,自定义单元:
import Foundation
class CustomTableViewCell: UITableViewCell
{
required init(coder aDecoder: NSCoder)
{
fatalError("init(coder:) has not been implemented")
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?)
{
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.selectionStyle = UITableViewCellSelectionStyle.None
}
}
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
我正在用这个,它对我很有用。
cell?.selectionStyle = UITableViewCellSelectionStyle.None
推荐文章
- iPhone上UIView和UILabels的渐变
- keychain上的分发证书中缺少私钥
- 在实现API时,我如何避免在块中捕获自我?
- 如何创建一个Swift Date对象?
- Xcode 4在目标设备上说“finished running <my app>”——什么都没有发生
- 从另一个应用程序打开设置应用程序
- 快速提取正则表达式匹配
- 如何应用梯度的背景视图的iOS Swift应用程序
- 图书馆吗?静态的?动态吗?或框架?另一个项目中的项目
- 如何用SwiftUI调整图像大小?
- Xcode 6 gitignore文件应该包括什么?
- 如何在iPhone/iOS上删除电话号码的蓝色样式?
- 检测视网膜显示
- 如何在UIImageView中动画图像的变化?
- 如何从iPhone访问SOAP服务