【问题标题】:What deselect selected cell when navigationController pops a view?当navigationController弹出一个视图时,什么取消选择选定的单元格?
【发布时间】:2011-01-21 10:38:07
【问题描述】:

苹果提供的默认 NavigationController 模板,它有一个 navigationController 和一个表格。

如果你选择了一个单元格,一个新的视图会被推送到 navigationController 中,如果你弹出这个视图,选中的单元格会被自动取消高亮显示。

但是表格如何知道何时取消突出显示以及它如何知道选择了哪个单元格??

还是只是重新加载所有数据?

【问题讨论】:

    标签: uitableview uinavigationcontroller


    【解决方案1】:

    表格如何知道何时取消高亮显示

    您可以在选择处理程序中取消选择您的单元格:

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

    或在控制器的-viewWillAppear: 方法中重置选择

    它如何知道选择了哪个单元格?

    UITableView 有如下方法获取选中行的indexPath:

    - (NSIndexPath *)indexPathForSelectedRow
    

    【讨论】:

      【解决方案2】:

      对于 Swift 3.0

      override func viewWillAppear(_ animated: Bool) {
          super.viewWillAppear(true)
          if self.yourTableView.indexPathForSelectedRow != nil
          {
              self.yourTableView.deselectRow(at: self.yourTableView.indexPathForSelectedRow!, animated: true)
          }
      }
      

      这段代码也将避免崩溃...

      另外,在选择 TableViewCell 时将以下行添加到您正在推送的另一个 ViewController 中。

      self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true
      

      像魅力一样工作 =]

      希望对你有帮助..

      【讨论】:

        猜你喜欢
        • 2012-02-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-08
        • 1970-01-01
        • 1970-01-01
        • 2014-07-17
        • 1970-01-01
        相关资源
        最近更新 更多