当你点击UITableView中的一行时,该行会高亮显示并被选中。是否有可能禁用这个,所以点击一行什么都不做?
当前回答
你可以使用:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UITableView的row at index path方法。
你还可以用:
[tableView deselectRowAtIndexPath:indexPath animated:NO];
在tableview中didselectrowatindexpath方法。
其他回答
cell.selectionStyle = UITableViewCellSelectionStyleNone;
对我来说,以下方法效果不错:
tableView.allowsSelection = false
这意味着didselectrow#将无法工作。也就是说,触摸表中的一行,本身不会有任何作用。(因此,很明显,永远不会有选中动画。)
(请注意,如果在单元格上有UIButton或任何其他控件,当然这些控件仍然可以工作。你碰巧在表格单元格上的任何控件,都与UITableView允许你使用didSelectRowAt#“选择一行”的能力完全无关。)
另一点需要注意的是:当UITableView处于编辑模式时,这是无效的。要在编辑模式下限制单元格选择,请使用如下代码:
tableView.allowsSelectionDuringEditing = false
因为我最近读了这篇文章,它对我有帮助,我想发布另一个答案来巩固所有的答案(为子孙后代)。
所以,根据你想要的逻辑和/或结果,实际上有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
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
我已经看过了所有的答案,在我的用例中起作用的是:
tableView.allowSelection = false
public func tableView(_ tableView: UITableView, canFocusRowAt indexPath: IndexPath) -> Bool {
true
}
通过这种方式,表格保持可聚焦,用户可以滚动它的元素,但不能“按下/选择”它们。
简单地设置单元格。selectionStyle = .none将允许列表元素是可选择的(只是不留下灰色选择标记)。只是设置allowSelection = false会导致我的表无法聚焦。用户将无法滚动元素。
推荐文章
- 以编程方式创建segue
- 如何使用Xcode创建。ipa文件?
- 动态改变UILabel的字体大小
- 新的自动引用计数机制是如何工作的?
- 在iPhone上确定用户是否启用了推送通知
- 是否有可能禁用浮动头在UITableView与UITableViewStylePlain?
- 错误ITMS-9000:“冗余二进制文件上传。火车1.0版本已经有一个二进制版本上传。
- Swift -转换为绝对值
- 从父iOS访问容器视图控制器
- 自定义dealloc和ARC (Objective-C)
- 调整UITableView的大小以适应内容
- 在代码中为UIButton设置一个图像
- NSRange从Swift Range?
- UICollectionView中的单元格间距
- 我如何在我的iOS应用程序中每n分钟得到一个后台位置更新?