繁体   English   中英

如何禁用 UITableView 选择?

[英]How can I disable the UITableView selection?

当您点击UITableView中的一行时,该行将突出显示并被选中。 是否可以禁用此功能,因此点击一行什么也不做?

您所要做的就是使用以下任一方法在UITableViewCell实例上设置选择样式:

目标-C:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

斯威夫特 2:

cell.selectionStyle = UITableViewCellSelectionStyle.None

斯威夫特 3 和 4.x:

cell.selectionStyle = .none

此外,请确保您没有在您的表格视图委托中实现-tableView:didSelectRowAtIndexPath:或者如果您确实实现了它,则明确排除您不想采取任何操作的单元格。

更多信息在这里这里

对我来说,以下工作正常:

tableView.allowsSelection = false

这意味着didSelectRowAt#根本不起作用。 也就是说,触摸桌子上的一行,绝对不会做任何事情。 (因此,显然,永远不会有选定的动画。)

(请注意,如果在单元格上有UIButton或任何其他控件,当然这些控件仍然可以工作。您在表格单元格上碰巧拥有的任何控件与 UITableView 允许您“选择一行”的能力完全无关" 使用didSelectRowAt# 。)

还有一点要注意:当UITableView处于编辑模式时,这不起作用。 要在编辑模式下限制单元格选择,请使用以下代码:

tableView.allowsSelectionDuringEditing = false 

因为我最近阅读了这篇文章并且它对我有帮助,所以我想发布另一个答案来整合所有答案(供后代使用)。



因此,实际上有 5 种不同的答案,具体取决于您想要的逻辑和/或结果:

1.要禁用蓝色突出显示而不更改单元格的任何其他交互:

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

当我有一个 UIButton - 或其他一些控件 - 托管在 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.userInteractionEnabled = NO;

除了阻止选择外,这还会阻止 tableView:didSelectRowAtIndexPath: 为设置了它的单元格调用。 (感谢 Tony Million 这个答案,谢谢!)

如果您的单元格中有需要点击的按钮,您需要改为:

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

并且您还需要忽略对- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath单元格的任何点击。

如果要禁用整个表的选择,请使用:

tableView.allowsSelection = NO;

(感谢 Paulo De Barros,谢谢!)

从 iOS 6.0 开始, UITableViewDelegatetableView:shouldHighlightRowAtIndexPath: (在 iOS 文档中阅读它。)

此方法可让您将特定行标记为不可突出显示(并且隐式地不可选择),而无需更改单元格的选择样式、使用userInteractionEnabled = NO单元格的事件处理或此处记录的任何其他技术。

您还可以通过从检查器窗格中的selection选项(UITableView 属性)中选择NoSelection来禁用界面构建器本身的行选择,如下图所示

UITableView 检查器

Swift 3 的固定解决方案

cell.selectionStyle = .none

在属性检查器中的UITableViewCellXIB中,将Selection值设置为None

在此处输入图片说明

如果有人需要Swift 的答案:

cell.selectionStyle = .None

如果你想让选择只闪烁,而不是保持在选择状态,你可以调用,在

didSelectRowAtIndexPath

以下

[tableView deselectRowAtIndexPath:indexPath animated:YES];

所以它会闪烁选择的状态并恢复。

这是我使用的,在cellForRowAtIndexPath编写此代码。:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

UITableViewDelegate协议中,您可以使用willSelectRowAtIndexPath方法,如果您不想选择该行,则return nil

以同样的方式,您可以使用willDeselectRowAtIndexPath方法,如果您不想取消选择该行,则return nil

1- 您所要做的就是使用以下任一方法UITableViewCell实例设置选择样式


目标-C:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];


斯威夫特 2:

cell.selectionStyle = UITableViewCellSelectionStyle.None


斯威夫特 3:

cell.selectionStyle = .none


2 - 不要实施 - tableView:didSelectRowAtIndexPath:在您的表格视图delegate或者如果您确实实施了它,则明确排除您不想采取任何行动的单元格。

3 - 此外,您也可以从情节提要中进行。 单击表格视图单元格,然后在表格视图单元格下的属性检查器中,将选择旁边的下拉列表更改为无。


4 - 您可以在 (iOS) Xcode 9 , Swift 4.0 中使用以下代码禁用表格单元格突出显示

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


        let cell = tableView.dequeueReusableCell(withIdentifier: "OpenTbCell") as! OpenTbCell
        cell.selectionStyle = .none
        return cell


}

目标-C:

  1. 下面的代码段禁用突出显示,但它也禁用了对didSelectRowAtIndexPath的调用。 因此,如果您没有实现didSelectRowAtIndexPath使用以下方法。 这应该在您创建表时添加。 不过,这将适用于单元格内的按钮和UITextField

     self.tableView.allowsSelection = NO;
  2. 下面的代码段禁用突出显示,它不会禁用对didSelectRowAtIndexPath的调用。 cellForRowAtIndexPath设置cell的选择样式为None

     cell.selectionStyle = UITableViewCellSelectionStyleNone;
  3. 下面的代码段禁用单元格上的所有内容。 这将禁用与buttonstextfields的交互:

     self.tableView.userInteractionEnabled = false;

斯威夫特:

以下是上述Objective-C解决方案的Swift等价物:

  1. 更换第一种解决方案

    self.tableView.allowsSelection = false
  2. 更换第二溶液

    cell?.selectionStyle = UITableViewCellSelectionStyle.None
  3. 更换第三种溶液

    self.tableView.userInteractionEnabled = false

尝试输入:

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)
}

我也一直在努力解决这个问题,在我的UITableViewCell有一个控件禁止使用userInteractionEnabled属性。 我有一个用于设置的 3 个单元格静态表,2 个带有日期,1 个带有开/关开关。 在 Storyboard/IB 中播放后,我设法使底部的不可选择,但是当您点击它时,顶部行之一的选择消失了。 这是我的设置 UITableView 的 WIP 图像:

设置 UITableView

如果您点击第三行什么也没有发生,选择将保留在第二行。 该功能实际上是 Apple 日历应用的添加事件时间选择屏幕的副本。

代码非常兼容,一直到 IOS2 =/:

- (NSIndexPath *)tableView: (UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 2) {
        return nil;
    }
    return indexPath;
}

这与将选择样式设置为无结合使用,因此单元格不会在触摸事件时闪烁

您只需将此代码放入 cellForRowAtIndexPath

禁用单元格的选择属性:(点击单元格时)。

cell.selectionStyle = UITableViewCellSelectionStyle.None

我们可以像这样写代码

 cell.selectionStyle = UITableViewCellSelectionStyleNone;

但是当我们在行上方有自定义单元格 xib 时会发出警告

自定义单元格 xib

我们需要从界面构建器中设置选择样式 None

试试这个

cell.selectionStyle = UITableViewCellSelectionStyleNone;

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

您还可以使用 interfacebuilder 设置选择样式。

我正在使用这个,这对我有用。

cell?.selectionStyle = UITableViewCellSelectionStyle.None

UITableViewDataSource协议,在方法cellForRowAt添加:

let cell = tableView.dequeueReusableCell(withIdentifier: "YOUR_CELL_IDENTIFIER", for: indexPath)                
cell.selectionStyle = .none
return cell

您可以转到 Storyboard > Select Cell > Identity Inspector > Selection 并从下拉列表中选择 none。

斯威夫特 3,4 和 5

更好的实践,在UITableViewCell编写代码

例如,你有一个名为MyCell UITableViewCell ,在awakeFromNib只写self.selectionStyle = .none

完整示例:

class MyCell: UITableViewCell {
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.selectionStyle = .none
    }
    
}

虽然这是防止行在选择期间显示突出显示的最佳和最简单的解决方案

cell.selectionStyle = UITableViewCellSelectionStyleNone;

我还想建议,偶尔简要显示该行已被选中然后将其关闭是有用的。 这会提醒用户确认他们打算选择的内容:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
     [tableView deselectRowAtIndexPath:indexPath animated:NO];
...
}

直接禁用 TableViewCell 突出显示到情节提要中

在此处输入图片说明

禁用 UItableviewcell 的突出显示

cell.selectionStyle = UITableViewCellSelectionStyleNone;

并且不应允许用户与单元格交互。

cell.userInteractionEnabled = NO;

您还可以将背景颜色设置为 Clear 以实现与UITableViewCellSelectionStyleNone相同的效果,以防您不想/不能使用UITableViewCellSelectionStyleNone

您将使用如下代码:

UIView *backgroundColorView = [[UIView alloc] init];
backgroundColorView.backgroundColor = [UIColor clearColor];
backgroundColorView.layer.masksToBounds = YES;
[cell setSelectedBackgroundView: backgroundColorView];

当您向每个单元格添加额外的彩色视图时,这可能会降低您的性能。

您可以使用:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

在 UITableView 的索引路径方法的行单元格中。

你也可以使用:

[tableView deselectRowAtIndexPath:indexPath animated:NO];

在 tableview didselectrowatindexpath 方法中。

您也可以从情节提要中执行此操作。 单击表格视图单元格,然后在表格视图单元格下的属性检查器中,将选择旁边的下拉列表更改为无。

你可以用这个

cell.selectionStyle = UITableViewCellSelectionStyleNone;

最好的解决方案是使选择样式无

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

但是,这里我们考虑的事实是没有用于选定状态的自定义图像。

您可以使用....

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

当您点击UITableView中的一行时,该行将突出显示并选中。 是否可以禁用此功能,所以点击一行不会执行任何操作?

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
[cell setSelected:NO animated:NO];
[cell setHighlighted:NO animated:NO];

快乐的编码!

带有自定义单元格的 Swift 解决方案:

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
  } 
}

您可以使用 UITableViewCell 的selectionStyle属性

 cell.selectionStyle = UITableViewCellSelectionStyleNone;

或者

 [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

另外,不要在委托下实现

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { ... }

如果您已经创建了 Xib/Storyboard 文件,那么您可以通过取消选中它来将 tableview 的setUserInteractionEnabled属性更改为No。 这将使您的 tableview 为只读。

您可以在 (iOS) Xcode 9 , Swift 4.0 中使用以下代码禁用表格单元格突出显示

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


        let cell = tableView.dequeueReusableCell(withIdentifier: "OpenTbCell") as! OpenTbCell
        cell.selectionStyle = .none
        return cell


}

禁用 UITableView 中所有 UITableViewCell 的选择

tableView.allowsSelection = false

禁用特定 UITableViewCells 的选择

cell.selectionStyle = UITableViewCell.SelectionStyle.none

更好的方法是:

cell.userInteractionEnabled = NO;

这种方法不会调用didSelectRowAtIndexPath:方法。

至少从 iOS 6 开始,您可以覆盖自定义单元格中的方法以防止蓝色突出显示。 没有其他交互被禁用或影响。 所有三个都必须被覆盖。

- (void) setHighlighted:(BOOL)highlighted
{
}

- (void) setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
}

- (void) setSelected:(BOOL)selected animated:(BOOL)animated
{
}

很简单的东西。 在返回 tableview Cell 之前,使用 table view cell 的 style 属性。

在返回表格视图单元格之前只需编写这行代码
cell.selectionStyle = .none

场景 - 1

如果您不想选择tableview上的某些特定单元格,您可以在cellForRow函数中为这些单元格设置选择样式。

目标-C

cell.selectionStyle = UITableViewCellSelectionStyleNone;

斯威夫特 4.2

cell.selectionStyle = .none

场景 - 2

要禁用整个表视图的选择:

目标-C

self.tableView.allowsSelection = false;

斯威夫特 4.2

self.tableView.allowsSelection = false

我已经完成了所有答案,对我的用例有用的是以下内容:

tableView.allowSelection = false
public func tableView(_ tableView: UITableView, canFocusRowAt indexPath: IndexPath) -> Bool {
    true
}

这样表格保持可聚焦,用户可以滚动浏览其元素但无法“按下/选择”它们。

简单地设置cell.selectionStyle = .none将允许列表元素是可选的(只是不留下灰色选择标记)。 并且仅设置allowSelection = false会导致我的表不可聚焦。 用户将无法滚动浏览元素。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM