当你点击UITableView中的一行时,该行会高亮显示并被选中。是否有可能禁用这个,所以点击一行什么都不做?
当前回答
你可以使用:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UITableView的row at index path方法。
你还可以用:
[tableView deselectRowAtIndexPath:indexPath animated:NO];
在tableview中didselectrowatindexpath方法。
其他回答
因为我最近读了这篇文章,它对我有帮助,我想发布另一个答案来巩固所有的答案(为子孙后代)。
所以,根据你想要的逻辑和/或结果,实际上有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
试着输入:
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)
}
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
快速解决方案,自定义单元:
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
}
}
你可以使用:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UITableView的row at index path方法。
你还可以用:
[tableView deselectRowAtIndexPath:indexPath animated:NO];
在tableview中didselectrowatindexpath方法。
推荐文章
- 自定义dealloc和ARC (Objective-C)
- 调整UITableView的大小以适应内容
- 在代码中为UIButton设置一个图像
- NSRange从Swift Range?
- UICollectionView中的单元格间距
- 我如何在我的iOS应用程序中每n分钟得到一个后台位置更新?
- 如何使用iOS创建GUID/UUID
- 禁用所呈现视图控制器的交互式撤销
- 点击按钮时如何打开手机设置?
- 如何使用UIVisualEffectView来模糊图像?
- 如何修复UITableView分隔符在iOS 7?
- 故事板中的自定义单元格行高设置没有响应
- 在Swift中使用自定义消息抛出错误/异常的最简单方法?
- 如何在Swift中获得唯一的设备ID ?
- 我如何知道何时UITableView完成了ReloadData?