当你点击UITableView中的一行时,该行会高亮显示并被选中。是否有可能禁用这个,所以点击一行什么都不做?
当前回答
快速解决方案,自定义单元:
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
}
}
其他回答
斯威夫特3,4和5
更好的做法是,在UITableViewCell中编写代码
例如,你有名为MyCell的UITableViewCell, 在awakeFromNib中只写self。selectionStyle = .none
完整的例子:
class MyCell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
self.selectionStyle = .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.selected = NO;
它将在需要时取消选择您的行。
在Swift3中…
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let r = indexPath.row
print("clicked .. \(r)")
tableView.cellForRow(at: indexPath)?.setSelected(false, animated: true)
}
因为我最近读了这篇文章,它对我有帮助,我想发布另一个答案来巩固所有的答案(为子孙后代)。
所以,根据你想要的逻辑和/或结果,实际上有5个不同的答案:
1.禁用蓝色高亮,而不改变单元格的任何其他交互作用:
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
我使用这个当我有一个UIButton -或其他一些控件(s) -托管在UITableViewCell,我希望用户能够与控件交互,而不是单元格本身。
注意:正如Tony Million上面提到的,这并不阻止tableView:didSelectRowAtIndexPath:。我通过简单的“if”语句来解决这个问题,最常见的是测试部分并避免对特定部分执行操作。
我想到的另一种测试敲击单元格的方法是:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// A case was selected, so push into the CaseDetailViewController
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.selectionStyle != UITableViewCellSelectionStyleNone) {
// Handle tap code here
}
}
2.要对整个表执行此操作,您可以将上述解决方案应用到表中的每个单元格,但您也可以这样做:
[tableView setAllowsSelection:NO];
在我的测试中,这仍然允许UITableViewCell内的控件是交互式的。 3.要使单元格为“只读”,你可以简单地这样做:
[cell setUserInteractionEnabled:NO];
4.将整个表设置为“只读”
[tableView setUserInteractionEnabled:NO];
5.要立即确定是否突出显示一个单元格(根据这个答案隐式地包括选择),你可以实现以下UITableViewDelegate协议方法:
- (BOOL)tableView:(UITableView *)tableView
shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
场景- 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
推荐文章
- 如何删除默认的导航栏空间在SwiftUI导航视图
- 如何在iOS中使用Swift编程segue
- Swift -整数转换为小时/分钟/秒
- Swift:声明一个空字典
- 在成功提交我的应用程序后,“太多符号文件”
- 首先添加一个UIView,甚至是导航栏
- 我如何改变UIButton标题颜色?
- 如何从UIImage (Cocoa Touch)或CGImage (Core Graphics)获取像素数据?
- 在Swift中如何调用GCD主线程上的参数方法?
- NSLayoutConstraints是可动画的吗?
- iOS -构建失败,CocoaPods无法找到头文件
- CFNetwork SSLHandshake iOS 9失败
- 请求失败:不可接受的内容类型:文本/html使用AFNetworking 2.0
- 缺少推荐的图标文件-该包不包含iPhone / iPod Touch的应用程序图标,像素为“120x120”,png格式
- 以编程方式创建segue