当你点击UITableView中的一行时,该行会高亮显示并被选中。是否有可能禁用这个,所以点击一行什么都不做?
当前回答
你可以使用UITableViewCell的selectionStyle属性
cell.selectionStyle = UITableViewCellSelectionStyleNone;
或
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
同样,不要实现下面的委托
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { ... }
如果你已经创建了Xib/Storyboard文件,那么你可以改变setUserInteractionEnabled 通过取消勾选tableview的属性为No。 这将使你的表视图为只读。
其他回答
编辑:对于更新的Swift,它被更改为:
cell.selectionStyle = .none
更多信息请看这个: https://developer.apple.com/documentation/uikit/uitableviewcell/selectionstyle
如果有人需要斯威夫特的答案:
cell.selectionStyle = .None
我们可以编写这样的代码
cell.selectionStyle = UITableViewCellSelectionStyleNone;
但当我们有自定义单元格xib时,在该行上面给出警告
自定义单元格xib
我们需要从界面构建器中设置选择样式为None
我已经看过了所有的答案,在我的用例中起作用的是:
tableView.allowSelection = false
public func tableView(_ tableView: UITableView, canFocusRowAt indexPath: IndexPath) -> Bool {
true
}
通过这种方式,表格保持可聚焦,用户可以滚动它的元素,但不能“按下/选择”它们。
简单地设置单元格。selectionStyle = .none将允许列表元素是可选择的(只是不留下灰色选择标记)。只是设置allowSelection = false会导致我的表无法聚焦。用户将无法滚动元素。
这就是我所使用的,在cellForRowAtIndexPath中编写这段代码。
cell.selectionStyle = 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
}
}
推荐文章
- 如何使用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