当你点击UITableView中的一行时,该行会高亮显示并被选中。是否有可能禁用这个,所以点击一行什么都不做?
当前回答
禁用UITableView中所有uitableviewcell的选择
tableView.allowsSelection = false
禁用特定uitableviewcell的选择
cell.selectionStyle = UITableViewCell.SelectionStyle.none
其他回答
场景- 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
你所要做的就是在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.selectionStyle = UITableViewCellSelectionStyleNone;
我还想建议,偶尔可以简单地显示已选中的行,然后将其关闭。这将提醒用户确认他们打算选择的内容:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
...
}
快速解决方案,自定义单元:
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
}
}
这就是我所使用的,在cellForRowAtIndexPath中编写这段代码。
cell.selectionStyle = UITableViewCellSelectionStyleNone;
推荐文章
- iPhone上UIView和UILabels的渐变
- keychain上的分发证书中缺少私钥
- 在实现API时,我如何避免在块中捕获自我?
- 如何创建一个Swift Date对象?
- Xcode 4在目标设备上说“finished running <my app>”——什么都没有发生
- 从另一个应用程序打开设置应用程序
- 快速提取正则表达式匹配
- 如何应用梯度的背景视图的iOS Swift应用程序
- 图书馆吗?静态的?动态吗?或框架?另一个项目中的项目
- 如何用SwiftUI调整图像大小?
- Xcode 6 gitignore文件应该包括什么?
- 如何在iPhone/iOS上删除电话号码的蓝色样式?
- 检测视网膜显示
- 如何在UIImageView中动画图像的变化?
- 如何从iPhone访问SOAP服务