【问题标题】:Disable selection of a single UITableViewCell禁用单个 UITableViewCell 的选择
【发布时间】:2011-04-05 22:08:34
【问题描述】:

如何禁止在 UITableView 中仅选择单个单元格?我有几个,我只想禁用最后一个。

【问题讨论】:

标签: ios iphone cocoa-touch uitableview


【解决方案1】:

如果有人想知道如何快速实现这一点,那么这是我的代码。我正在使用 Xcode 7 并使用 iPad Retina(iOS 9) 进行测试。

cell.selectionStyle = UITableViewCellSelectionStyle .None
cell.userInteractionEnabled = false

无论你想要,尝试放置这两行代码。就我而言,我已在此方法中使用它来显示单元格。

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath)

请记住,这两行代码会阻止对您的单元格进行任何类型的选择或交互,但您只能根据需要单独使用第一行。那就是……

cell.selectionStyle = UITableViewCellSelectionStyle .None

只有这一行会阻止选择您的单元格。

但是,第二行将使单元格“只读”。那就是..

cell.userInteractionEnabled = false

谢谢

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    快速尝试一下:

    self.tableView.deselectRowAtIndexPath(indexPath, animated: true)
    

    【讨论】:

      【解决方案3】:

      使用 iOS 6。

      您可以使用以下委托方法并返回 NO 以防您不选择它,并返回 YES 以防您希望它被选择。

      - (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
      {
          return indexPath.section == 0;
      }
      

      【讨论】:

        【解决方案4】:

        要停止选择某些单元格,请使用:

        cell.userInteractionEnabled = NO;
        

        除了阻止选择之外,这还会停止为设置了它的单元格调用 tableView:didSelectRowAtIndexPath:。它还将使画外音将其视为暗淡按钮(可能是也可能不是您想要的)。

        请注意,如果单元格中有交互式元素(即开关/按钮),则需要使用 cell.selectionStyle = UITableViewCellSelectionStyleNone; 代替,然后确保忽略对 tableView:didSelectRowAtIndexPath: 中单元格的点击。

        【讨论】:

        • 尝试在接收突出显示的视图上执行此操作。否则,与禁用单元格相同。
        • 我不能使用它,因为它还会禁用cell.accessoryView,我已将其设置为我希望可点击的UIButton
        • 此外,这会导致 Voice Over 错误地将单元格视为暗色按钮而不是静态文本(带有可选附件)。
        • @Endersstocker 谢谢,这是有用的额外信息——我在回答中提到了这一点,所以它更加突出。 (我猜在某些情况下可能需要人们想要,这取决于他们为什么要禁用单元格?)
        【解决方案5】:

        我发现的最干净的解决方案只使用委托方法 willDisplayCell。

        - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
        {
            if([indexPath row] == 0) //<-----ignores touches on first cell in the UITableView
            {                        //simply change this around to suit your needs
                cell.userInteractionEnabled = NO;
                cell.textLabel.enabled = NO;
                cell.detailTextLabel.enabled = NO;
            }
        }
        

        您不必在委托方法 didSelectRowAtIndexPath 中采取任何进一步的操作来确保忽略此单元格的选择。对此单元格的所有触摸都将被忽略,单元格中的文本也将变灰。

        【讨论】:

          【解决方案6】:

          将其放入您的自定义表 VC 中:

          // cells lacking UITableViewCellAccessoryDisclosureIndicator will not be selectable
          - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
          {
              UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
              if (cell.accessoryType != UITableViewCellAccessoryDisclosureIndicator) {
                  return nil;
              }
              return indexPath;
          }
          
          // disabled cells will still have userinteraction enabled for their subviews
          - (void)setEnabled:(BOOL)enabled forTableViewCell:(UITableViewCell *)tableViewCell
          {
              tableViewCell.accessoryType = (enabled) ? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
              // if you dont want the blue selection on tap, comment out the following line
              tableViewCell.selectionStyle = (enabled) ? UITableViewCellSelectionStyleBlue : UITableViewCellSelectionStyleNone;
          }
          

          然后要启用/禁用 someTableViewCell 的选择,请执行以下操作:

          [self setEnabled:state forTableViewCell:someTableViewCell];
          

          大功告成,可以发货了。

          【讨论】:

            【解决方案7】:

            正如我提到的in another thread,上述所有方法都不能精确地解决问题。禁用单元格的正确方法是通过方法

            - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
            

            在那个方法中,必须使用

            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            

            禁用单元格选择,但仍然允许用户与单元格的子视图进行交互,例如 UISwitch。

            【讨论】:

              【解决方案8】:
              -(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
              {
                  if ([self numberOfRowsInSection] == [indexPath row]) {
                      return nil;
                  } else {
                      return indexPath;
                  }
              }
              

              表格的最后一行不会被选中

              【讨论】:

              • 这将阻止单元格被实际选中,但是当用户触摸它时它仍然会突出显示
              • 对不起,需要给单元格添加 UITableViewCellSelectionStyleNone 样式
              • 我可能会补充一点,设置 tableview 选择样式应该在 cellForRowAtIndexPath: 中完成,以避免加载 TableView 时出现延迟(table view 需要 1-2 才能禁用单元格的选择)。跨度>
              【解决方案9】:
              - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
              {
                  UITableViewCell *cell = ...
              
                  cell.selectionStyle = UITableViewCellSelectionStyleNone;
              
              }
              

              【讨论】:

              • 这将阻止单元格看起来被选中,但仍会调用tableView:didSelectRowAtIndexPath:
              • 结合使用cell.selectionStyle = .Nonecell.userInteractionEnabled = false 将涵盖大多数情况。
              猜你喜欢
              • 2011-06-08
              • 1970-01-01
              • 2017-04-07
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-10-11
              相关资源
              最近更新 更多