【问题标题】:Why is -didDeselectRowAtIndexPath not being called?为什么没有调用 -didselectRowAtIndexPath?
【发布时间】:2012-04-06 20:38:55
【问题描述】:

我创建了一个新项目(Xcode 4,Master-Detail 应用程序)只是为了看看我是否做错了什么,但我仍然遇到同样的问题。我想在用户取消选择一个单元格时调用-reloadData,所以这是我的代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"%s", __PRETTY_FUNCTION__);
}

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%s", __PRETTY_FUNCTION__);
    [tableView reloadData];
}

-(NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%s", __PRETTY_FUNCTION__);
    return indexPath;
}

问题是 didDeselectRowAtIndexPathwillDeselectRowAtIndexPath 似乎没有被调用。这是预期的行为吗? tableView:didDeselectRowAtIndexPath: 状态的文档

告诉代理指定的行现在被取消选择。

所以我想它应该像我想的那样工作。

【问题讨论】:

  • 您是否为您的 UITableView 正确设置了委托?
  • 您是否指定了表委托?您是否允许用户选择单元格?
  • 是的,是的。选择正常工作,取消选择是问题..

标签: ios uitableview


【解决方案1】:

如果调用deselectRowAtIndexPath:animated:,则不会发送代理方法tableView:willDeselectRowAtIndexPath:tableView:didDeselectRowAtIndexPath:消息。

【讨论】:

  • 手势识别器呢?如果我们使用deselectRowAtIndexPath:animated:,会执行吗?
  • tableView:willDeselectRowAtIndexPath: 也没有被调用。我正在使用 TableViewcontroller
  • 哇非常感谢只有这个解决方案对我有用
【解决方案2】:

tableView:willDeselectRowAtIndexPath: 的文档也这么说

仅当存在现有选择时才调用此方法 用户尝试选择不同的行。向委托发送此方法 对于先前选择的行。您可以使用 UITableViewCellSelectionStyleNone 禁用外观 触地时单元格突出显示。

当我使用 UITableViewCellSelectionStyleNone 时,它对我们不起作用。

【讨论】:

  • 但是这是给willDeselectRowAtIndexPath的,不是给didDeselectRowAtIndexPath的,对吧??是不是只有在选择了不同的单元格后才会触发两者?
  • 这两个都用,检查UITableViewDelegate Protocol Reference
  • 好的,但是如果我们忘记 willDeselectRowAtIndexPath,似乎我没有做错什么,而 didDeselectRowAtIndexPath 只是没有按照我想要的方式工作,对吧?
  • 这个答案看起来不对。 UITableViewCellSelectionStyleNone 不会阻止 willDeselectRowAtIndexPathdidDeselectRowAtIndexPath 被调用。
【解决方案3】:

我发现的另一个怪癖——无论如何是在 IOS 5.1 中——如果你在桌子上调用reloadData,你将不会得到任何选定行的didDeselectRowAtIndexPath。就我而言,我会根据是否选择单元格布局稍微调整单元格布局,因此我需要在重新加载表格数据之前手动完成这项工作。

【讨论】:

  • 你是对的。 reloadData 使 tableView “忘记”选定的行。然而,有一个快速的解决方法:只需调用 reloadRowsAtIndexPaths:withAnimation: 重新加载特定的行
【解决方案4】:

我知道这是一个老问题,但我遇到了同样的问题。

如果你使用

[tableView reloadData]

然后重新加载表数据,后台没有选择行-意思

只有

didSelectRowAtIndexPath

曾经被调用过。我希望这对遇到此问题的人有所帮助。

【讨论】:

    【解决方案5】:

    解决此问题的一种简洁方法是执行以下操作:

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath)
        // Do your cell setup work here...
    
        //If your cell should be selected...
        if cellShouldBeSelected {
            tableView.selectRowAtIndexPath(indexPath, animated: false, scrollPosition: UITableViewScrollPosition.None)
        }
    
        return cell
    }
    

    这解决了在tableView.reloadData()call 发生后单元格没有响应被取消选择的整个问题。

    【讨论】:

    • 我不是特别喜欢 Swift,但是你的建议真的帮了我很大的忙,非常感谢!
    • 不错!谢谢你。
    【解决方案6】:

    第一次选择任何单元格时,不会调用-[UITableViewDelegate tableView:didDeselectRowAtIndexPath:] 方法,而是调用-[UITableViewDelegate tableView:didSelectRowAtIndexPath:]。再选择一个单元格后,didDeselectRowAtIndexPathdidSelectRowAtIndexPath 之后立即被调用。

    没关系。

    但是如果你必须在开始时显示一个单元格(eq 使用UITableViewCellAccessoryCheckmark),那么,在选择另一个单元格后,你可能希望第一次调用didDeselectRowAtIndexPath 方法,以取消选择前一个单元格.

    解决方案!

    您必须在-[UITableViewDataSource tableView:cellForRowAtIndexPath:] 中调用-[UITableView selectRowAtIndexPath:animated:scrollPosition:] 以通知已选择所需单元格。

    Objective-C

    #pragma mark - UITableViewDelegate
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        // saving the current selected row
        SelectedRow = indexPath.row;
    }
    
    - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    #pragma mark - UITableViewDataSource
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
    
        // preventing selection style
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
        cell.textLabel.text = "Some text";
    
        if (indexPath.row == SelectedRow) {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
            // just wanted to make it selected, but it also can scroll to the selected position
            [tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
        }
    
        return cell;
    }
    

    斯威夫特 3.1

    // MARK: - UITableViewDelegate
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cell = tableView.cellForRow(at: indexPath)!
        cell.accessoryType = UITableViewCellAccessoryType.checkmark
        selectedRow = indexPath.row
    }
    
    override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        let cell = tableView.cellForRow(at: indexPath)!
        cell.accessoryType = UITableViewCellAccessoryType.none
    }
    
    // MARK: - UITableViewDataSource
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
    
        // preventing selection style
        cell.selectionStyle = UITableViewCellSelectionStyle.none
    
        cell.textLabel?.text = "some text"
    
        if (indexPath.row == selectedRow) {
            cell.accessoryType = UITableViewCellAccessoryType.checkmark
            // just wanted to make it selected, but it also can scroll to the selected position
            tableView.selectRow(at: indexPath, animated: false, scrollPosition: UITableViewScrollPosition.none)
        }
    
        return cell
    }
    

    【讨论】:

      【解决方案7】:

      您只需将选择设置为“多选”,因为 Xcode 仅允许您在此模式下取消选择单元格。

      Xcode Screenshot

      【讨论】:

        【解决方案8】:

        将该表视图的allowsMultipleSelection 设置为TRUE

          self.tableView.allowsMultipleSelection = YES;
        

        【讨论】:

          【解决方案9】:

          我认为这只是一个简单的错误! 您为什么不使用以下内容:

          [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
          

          而不是在该行中仅使用“tableView”?
          我想,并且很确定上面的行会给你解决方案!
          希望这对你有所帮助,如果你回头看看你的老问题!!!
          赞! :)

          【讨论】:

          • 我不认为这是问题所在,tableView 是指 self.tableView 反正。试试看,你会学到一些东西。
          • :?好的,如果我有误导,对不起!我会尝试在此处将调查结果作为评论发布!感谢您的建议!
          【解决方案10】:

          对我来说,它通过添加 ->super.setSelected(selected, animated: animated)

          开始工作
          override func setSelected(_ selected: Bool, animated: Bool) {
             super.setSelected(selected, animated: animated)//This was missing
          }
          

          【讨论】:

            【解决方案11】:

            首先,您必须将allowsMultipleSelection 设置为true 并通过以下代码设置delegate

            self.tableView.allowsMultipleSelection = true
            self.tableView.delegate = self
            

            如果您在didSelectRowAt 委托方法中使用tableView.reloadData(),请将其删除。

            在您的自定义单元格中,使用选定的方法。

            override func setSelected(_ selected: Bool, animated: Bool) {
               super.setSelected(selected, animated: animated)
               //write your code here for selection method
            }
            

            通过这种方法,您的单元格状态被选中。如果您再次单击选定的单元格,则 didDeSelect 委托方法将自动调用。

            【讨论】:

              猜你喜欢
              • 2013-10-07
              • 1970-01-01
              • 1970-01-01
              • 2023-04-01
              • 2015-05-17
              • 2015-02-22
              • 1970-01-01
              相关资源
              最近更新 更多