【问题标题】:Removing UITableViewCell Selection删除 UITableViewCell 选择
【发布时间】:2023-03-27 17:31:01
【问题描述】:

目前我正在使用UITableViewSelectionStyleNone 覆盖标准 UITableViewSelectionStyle,然后根据委托方法更改单元格的颜色:

- (void)tableView:(UITableView *)tableView 
      didHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];                
    [cell setBackgroundColor:[UIColor yellowColor]];
}

- (void)tableView:(UITableView *)tableView 
    didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell setBackgroundColor:[UIColor whiteColor]];
}

- (void)tableView:(UITableView *)tableView 
    didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"indexpath: %i",indexPath.row);
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell setBackgroundColor:[UIColor whiteColor]];
}

- (void)tableView:(UITableView *)tableView 
    didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell setBackgroundColor:[UIColor whiteColor]];
}

这几乎可以正常工作,除了每当我突出显示一个单元格然后将手指从其上拖开而不实际选择它时,颜色不会变为白色......如果我将它设置为 [UIColor RedColor] 它会完美运行。这是为什么……

编辑:

当我在 didUnhlightRowAtIndexPath 之后打印出 indexPath.row 时,我从我的 NSLog 中得到“indexpath: 2147483647”

【问题讨论】:

  • indexPath 2147483647 等价于NSNotFound

标签: ios objective-c uitableview


【解决方案1】:

你可以试试:

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

如果您只是希望在选择单元格后高亮消失。除非我误解了你的问题。

【讨论】:

    【解决方案2】:

    你也可以试试这个

    tableView.allowsSelection = NO;
    

    另一种方法

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    

    还有一个

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

    【讨论】:

      【解决方案3】:

      您也可以从情节提要中执行此操作。选择 tableViewCell 并在 Attributes Inspector 下选择 Selection > None

      【讨论】:

        【解决方案4】:

        这是Swift 2 version

            if let indexPaths = self.tableView.indexPathsForSelectedRows {
                for indexPath in indexPaths {
                    self.tableView.deselectRowAtIndexPath(indexPath, animated: true)
                }
            }
        

        【讨论】:

          【解决方案5】:

          取消选择选定的行。你甚至不需要知道它是哪一个。

          tableView.selectRow(at: nil, animated: true, scrollPosition: .none)
          

          【讨论】:

            【解决方案6】:

            通过维护我的 indexPath 的本地实例,我能够找到最后选择的单元格并将其颜色更改为白色。我必须自己保持状态似乎真的很烦人,但事实就是这样......

            【讨论】:

            • 不知道为什么我投了反对票...只是解释我如何解决自己的问题以造福 Stack 上的其他人...
            • 是的,当这种情况发生时,我感到你的痛苦并讨厌它。+1 为你的问题和回答我的朋友 :)
            • 您不必维护状态。 UITableView 有方法indexPathsForSelectedRows
            【解决方案7】:

            对我有用的最简单的解决方案(Swift 4.2)
            (如果您仍然希望表格视图的行是可选的)

            override func viewWillAppear(_ animated: Bool) {
                super.viewWillAppear(animated)
                if let index = self.tableView.indexPathForSelectedRow{
                    self.tableView.deselectRow(at: index, animated: true)
                }
            }
            

            【讨论】:

              【解决方案8】:

              用扩展来做这个怎么样?

              import UIKit
              
              extension UITableView {
                  func removeRowSelections() {
                      self.indexPathsForSelectedRows?.forEach {
                          self.deselectRow(at: $0, animated: true)
                      }
                  }
              }
              

              用法:

              tableView.removeRowSelections()
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2011-04-06
                • 2011-06-22
                • 1970-01-01
                • 1970-01-01
                • 2015-03-12
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多