【问题标题】:How to make a cell on a UITableView not selectable?如何使 UITableView 上的单元格不可选?
【发布时间】:2011-02-08 03:36:51
【问题描述】:

我有一个要插入到 UITableView 顶部的单元格。如何确保当用户单击单元格时,它不会显示蓝色选定指示器?

【问题讨论】:

    标签: iphone objective-c cocoa-touch uitableview


    【解决方案1】:

    要取消根据行索引选择任何表格单元格或特定表格单元格的功能,请使用willSelectRowAt

    func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
    
    
            return nil
        }
    

    要简单地移除选择元素的 UI 效果,请将 selection style of the UITableViewCell 设置为 UITableViewCellSelectionStyleNone

    斯威夫特 5:

    selectionStyle = .none
    

    【讨论】:

    • 这不会使单元格不可选择,它只是意味着单元格的视觉外观在被选中时不会改变。 tableView:didSelectRowAtIndexPath: 在点击单元格时仍会被调用。要真正防止选择,您需要在 UITableViewDelegateimplement tableView:willSelectRowAtIndexPath: 并为不可选择的行返回 nil。
    • 斯威夫特 3 - cell.selectionStyle = UITableViewCellSelectionStyle.none
    • 或者简单地说:cell.selectionStyle = .none
    【解决方案2】:

    要在每个单元格的基础上使单元格完全不可选择,需要做两件事:

    1- 正如其他人所说:

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    

    2- 实现这个委托方法如下:

    // Called before the user changes the selection. Return a new indexPath, or nil, to change the proposed selection.
    - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
        if(cell.selectionStyle == UITableViewCellSelectionStyleNone){
            return nil;
        }
        return indexPath;
    }
    

    【讨论】:

      【解决方案3】:

      从情节提要中为表格视图设置以下内容:

      选择:无选择

      触摸时显示选择:错误

      【讨论】:

        【解决方案4】:

        你可以的

        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        

        【讨论】:

        • 少了一个点。这是固定版本: cell.selectionStyle = UITableViewCellSelectionStyle.None
        【解决方案5】:

        对于 Swift 3,您可以使用

         cell.isUserInteractionEnabled = false
        

        【讨论】:

        • 只是将代码添加到表格视图的 cellForRowAt indexPath 方法中,很简单
        • 当你想在表格视图上禁用用户交互时,这个方法很有用,否则你可以使用 Ben 下面的方法分享
        • 也适用于 Swift 4。
        • 它对我来说很好用。应该是正确答案。另一种方法是在tableView:didSelectRowAtIndexPath委托方法中,检测单元格索引,什么都不做,然后返回。
        • 我最喜欢这个答案的部分是您可以在单元格内声明它。如果您有一个单元格(例如一个空的状态单元格)不想响应点击,这是一个不错的选择。
        【解决方案6】:

        Swift 语法:

        cell.selectionStyle = UITableViewCellSelectionStyle.None
        

        【讨论】:

          【解决方案7】:

          我是从禁用 TableViewCell 的角度来回答的。 您可以使用情节提要。

          XCode Version 8.2.1 (8C1002)

          在情节提要上选择 TableVewCell,右侧面板将显示以下内容 - 实用程序。

          进行选择:无

          就是这样!

          【讨论】:

            【解决方案8】:

            问题是如何使某些单元格可选择而其他单元格不可选择。这是我的解决方案,(在尝试了很多其他建议之后):

            func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
                if (indexPath.row == 1 || indexPath.row == 2) {
                    return indexPath
                }
                return nil        
            }
            

            【讨论】:

              【解决方案9】:
              myTable.allowsSelection = false
              

              【讨论】:

                【解决方案10】:

                在 Swift 3 中更新:

                cell.selectionStyle = UITableViewCellSelectionStyle.none
                

                【讨论】:

                  【解决方案11】:

                  对于 Swift 3:

                  cell.selectionStyle = .none
                  

                  【讨论】:

                  • 正如上面所建议的“这不会使单元格不可选择它只是意味着单元格的视觉外观在被选中时不会改变。tableView:didSelectRowAtIndexPath: 当单元格仍然被调用被点击。要真正防止选择,您需要在 UITableViewDelegate 上实现 tableView:willSelectRowAtIndexPath: 并为不可选择的行返回 nil。-"
                  • @Siyavash 如原始问题所示,此人只想隐藏“蓝色选定指示器”。这与想要使其完全不可选择不同。简单地说,提出问题的人只是试图隐藏选择的视觉指示。
                  【解决方案12】:

                  实现这个methodUITableViewDelegate

                  - (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath 
                  {
                      return NO;
                  }
                  

                  【讨论】:

                    【解决方案13】:

                    斯威夫特 5

                    你也可以试试这个

                    tableView.allowsSelection = false
                    

                    【讨论】:

                      【解决方案14】:

                      斯威夫特 5

                        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                      
                          let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! UITableViewCell;
                          cell.selectionStyle = .none
                      }
                      

                      【讨论】:

                        猜你喜欢
                        • 2011-09-26
                        • 1970-01-01
                        • 1970-01-01
                        • 2011-04-27
                        • 1970-01-01
                        • 1970-01-01
                        • 2018-11-22
                        • 1970-01-01
                        • 1970-01-01
                        相关资源
                        最近更新 更多