当你点击UITableView中的一行时,该行会高亮显示并被选中。是否有可能禁用这个,所以点击一行什么都不做?
当前回答
编辑:对于更新的Swift,它被更改为:
cell.selectionStyle = .none
更多信息请看这个: https://developer.apple.com/documentation/uikit/uitableviewcell/selectionstyle
如果有人需要斯威夫特的答案:
cell.selectionStyle = .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
禁用UITableView中所有uitableviewcell的选择
tableView.allowsSelection = false
禁用特定uitableviewcell的选择
cell.selectionStyle = UITableViewCell.SelectionStyle.none
快速解决方案,自定义单元:
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
}
}
你所要做的就是在UITableViewCell实例上设置选择样式:
objective - c:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
or
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
斯威夫特2:
cell.selectionStyle = UITableViewCellSelectionStyle.None
Swift 3和4.x:
cell.selectionStyle = .none
此外,确保你没有在你的表视图委托中实现-tableView:didSelectRowAtIndexPath:或者显式地排除你想要没有动作的单元格。
更多信息在这里和这里
更好的方法是:
cell.userInteractionEnabled = NO;
这种方法不会调用didSelectRowAtIndexPath:方法。
推荐文章
- 如何使用Xcode创建。ipa文件?
- 动态改变UILabel的字体大小
- 新的自动引用计数机制是如何工作的?
- 在iPhone上确定用户是否启用了推送通知
- 是否有可能禁用浮动头在UITableView与UITableViewStylePlain?
- 错误ITMS-9000:“冗余二进制文件上传。火车1.0版本已经有一个二进制版本上传。
- Swift -转换为绝对值
- 从父iOS访问容器视图控制器
- 自定义dealloc和ARC (Objective-C)
- 调整UITableView的大小以适应内容
- 在代码中为UIButton设置一个图像
- NSRange从Swift Range?
- UICollectionView中的单元格间距
- 我如何在我的iOS应用程序中每n分钟得到一个后台位置更新?
- 如何使用iOS创建GUID/UUID