代码之家  ›  专栏  ›  技术社区  ›  davidmytton

如何禁用UITableView选择?

  •  1275
  • davidmytton  · 技术社区  · 17 年前

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

    42 回复  |  直到 7 年前
        1
  •  2002
  •   Sunil Targe    7 年前

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

    Objective-C:

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    

    Swift 2:

    cell.selectionStyle = UITableViewCellSelectionStyle.None
    

    Swift 3和4.x:

    cell.selectionStyle = .none
    

    此外,确保你要么不实施 -tableView:didSelectRowAtIndexPath: 在表视图中委托或显式排除您希望没有操作的单元格(如果您确实实现了它)。

    More info here here

        2
  •  643
  •   Let's_Create    7 年前

    对我来说,以下方法很有效:

    tableView.allowsSelection = false
    

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

    (请注意,如果在单元格上,您有 UIButton 当然,这些控件仍然有效。您在表格单元格上碰巧拥有的任何控件都与UITableView允许您使用以下命令“选择行”的能力完全无关 didSelectRowAt# .)

    另一点需要注意的是:当 UITableView 处于编辑模式。要在编辑模式下限制单元格选择,请使用以下代码:

    tableView.allowsSelectionDuringEditing = false 
    
        3
  •  360
  •   Community Mohan Dere    9 年前

    因为我最近读了这篇文章,它对我很有帮助,所以我想发布另一个答案来巩固所有的答案(供后人参考)。



    因此,根据你想要的逻辑和/或结果,实际上有5个不同的答案:

    1.要禁用蓝色突出显示而不更改单元格的任何其他交互,请执行以下操作:

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    

    当我在UITableViewCell中托管了UIButton或其他控件,并且我希望用户能够与控件交互,但不能与单元格本身交互时,我会使用此功能。

    笔记 正如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.即时确定是否突出显示单元格(根据 this answer 隐式包含选择),您可以实现以下功能 UITableViewDelegate 协议方法:

    - (BOOL)tableView:(UITableView *)tableView 
       shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
    
        4
  •  88
  •   JosephH    9 年前

    根据我自己的实施经验,总结一下我认为正确的答案:

    如果只想禁用部分单元格的选择,请使用:

    cell.userInteractionEnabled = NO;
    

    除了阻止选择外,这还会阻止对设置了tableView:didSelectRowAtIndexPath:的单元格调用tableView:did SelectRowAtindexPath:。(感谢Tony Million的回答!)

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

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    

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

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

    tableView.allowsSelection = NO;
    

    (感谢Paulo De Barros!)

        5
  •  58
  •   cbowns    13 年前

    从iOS 6.0开始, UITableViewDelegate tableView:shouldHighlightRowAtIndexPath: (在iOS中阅读 Documentation .)

    此方法允许您将特定行标记为不突出显示(以及隐式、不可选择),而无需更改单元格的选择样式,从而干扰单元格的事件处理 userInteractionEnabled = NO ,或此处记录的任何其他技术。

        6
  •  51
  •   vignesh kumar    12 年前

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

    UITableView Inspector

        7
  •  47
  •   Oscar Falmer    9 年前

    SWIFT 3的固定解决方案

    cell.selectionStyle = .none
    
        8
  •  37
  •   Zaid Pathan    9 年前

    EDIT:对于较新的Swift,它更改为:

    cell.selectionStyle = .none
    

    请查看此处以获取更多信息: https://developer.apple.com/documentation/uikit/uitableviewcell/selectionstyle

    如果有人需要答案 Swift :

    cell.selectionStyle = .None
    
        9
  •  36
  •   Aks    11 年前

    在你的 UITableViewCell s XIB 在属性检查器中设置值 Selection None .

    enter image description here

        10
  •  35
  •   Chris Fox    13 年前

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

    didSelectRowAtIndexPath
    

    以下内容

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    

    因此,它将闪烁所选状态并恢复。

        11
  •  30
  •   Community Mohan Dere    7 年前

    UITableViewDelegate 您可以使用该方法的协议 willSelectRowAtIndexPath return nil 如果你不想选中该行。

    同样,您可以使用 willDeselectRowAtIndexPath 方法与 返回nil 如果你不想取消选择该行。

        12
  •  29
  •   Paras Joshi    13 年前

    这就是我使用的,在 cellForRowAtIndexPath 写这段代码:

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
        13
  •  27
  •   heckj    7 年前

    1-你所要做的就是设置 选择风格 UITableViewCell 实例使用以下任一方式:


    Objective-C:

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    


    Swift 2:

    cell.selectionStyle = UITableViewCellSelectionStyle.None
    


    Swift 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
    
    
    }
    
        14
  •  20
  •   M Reza    8 年前

    Objective-C:

    1. 下面的代码段禁用突出显示,但也禁用对 didSelectRowAtIndexPath 。因此,如果您没有实施 didSelectRowAtIndexPath 然后使用以下方法。这应该在创建表时添加。这将适用于按钮和 UITextField 然而,在细胞内部。

      self.tableView.allowsSelection = NO;
      
    2. 下面的代码段禁用突出显示,但不会禁用对 didSelectRowAtIndexPath 。在中,将单元格的选择样式设置为“无” cellForRowAtIndexPath

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

      self.tableView.userInteractionEnabled = false;
      

    Swift:

    下面是 Swift 相当于上述 Objective-C 解决:

    1. 更换第一种解决方案

      self.tableView.allowsSelection = false
      
    2. 替换第二种解决方案

      cell?.selectionStyle = UITableViewCellSelectionStyle.None
      
    3. 替换第三种解决方案

      self.tableView.userInteractionEnabled = false
      
        15
  •  19
  •   Fattie    9 年前

    Swift 3,4和5

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

    例如,你有 UITableViewCell 与名称 MyCell , 在……里面 awakeFromNib 只要写 self.selectionStyle = .none

    完整示例:

    class MyCell: UITableViewCell {
        
        override func awakeFromNib() {
            super.awakeFromNib()
            self.selectionStyle = .none
        }
        
    }
    
        16
  •  14
  •   Yarek T Dima    13 年前

    尝试键入:

    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)
    }
    
        17
  •  11
  •   iDhaval    14 年前

    我也一直在与这种情况作斗争,对我的 UITableViewCell 禁止使用 userInteractionEnabled 财产。我有一个3单元格的静态表用于设置,2个带日期,1个带开/关开关。在Storyboard/IB中玩完之后,我设法使底部的一个不可选择,但当你点击它时,顶部一行的选择就会消失。这是我的设置UITableView的WIP图像:

    Settings UITableView

    如果您点击第三行,则什么都不会发生,选择将保留在第二行。该功能实际上是苹果日历应用程序添加事件时间选择屏幕的副本。

    该代码的兼容性令人惊讶,一直到IOS2=/:

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

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

        18
  •  11
  •   Gaurav Patel    10 年前

    我们可以编写如下代码

     cell.selectionStyle = UITableViewCellSelectionStyleNone;
    

    但是当我们有 自定义单元格xib 线上 警告 当时为

    自定义单元格xib

    我们需要…… 从界面生成器中设置选择样式“无”

        19
  •  10
  •   Deepak Swami    13 年前

    您只需将此代码放入cellForRowAtIndexPath中

    要禁用单元格的选择属性,请执行以下操作:(点击单元格时)。

    cell.selectionStyle = UITableViewCellSelectionStyle.None
    
        20
  •  10
  •   josliber Martin Ballet    10 年前

    来自 UITableViewDataSource 协议,内部方法 cellForRowAt 添加:

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

    您可以转到故事板>选择单元格>身份检查员>选择并从下拉列表中选择无。

        21
  •  9
  •   José    5 年前

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

    cell?.selectionStyle = UITableViewCellSelectionStyle.None
    
        22
  •  9
  •   Rashid Latif    5 年前

    试试这个

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    

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

        23
  •  7
  •   Steve Barden    12 年前

    直接禁用TableViewCell在故事板中的突出显示

    enter image description here

        24
  •  7
  •   Pang Ajmal PraveeN    8 年前

    虽然这是防止行在选择过程中显示高亮显示的最佳和最简单的解决方案

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    

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

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
         [tableView deselectRowAtIndexPath:indexPath animated:NO];
    ...
    }
    
        25
  •  6
  •   Sharme    13 年前

    禁用对UITableView中所有UITableViewCell的选择

    tableView.allowsSelection = false
    

    禁用对特定UITableViewCell的选择

    cell.selectionStyle = UITableViewCell.SelectionStyle.none
    
        26
  •  6
  •   virindh    12 年前

    禁用UItableview单元格的突出显示

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    

    并且不应允许用户与细胞进行交互。

    cell.userInteractionEnabled = NO;
    
        27
  •  6
  •   Cindy    11 年前

    您还可以将背景颜色设置为“清除”,以达到与相同的效果 UITableViewCellSelectionStyleNone ,以防你不想/不能使用 UITableViewCell选择样式无 .

    您将使用以下代码:

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

    这可能 退化 你的表现就像你添加了一个 额外的 彩色视图 每个 细胞。

        28
  •  6
  •   Sangram Shivankar Wostein    10 年前
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
        29
  •  5
  •   iEinstein    13 年前

    你也可以从故事板上做到这一点。单击表视图单元格,在表视图单元格下的属性检查器中,将“选择”旁边的下拉菜单更改为“无”。

        30
  •  5
  •   Mak083    12 年前

    您可以使用:

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    

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

    您还可以使用:

    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    

    在表视图中,我们选择了rowatindexpath方法。

    推荐文章