【问题标题】:When editing, `UITableView` does not call didSelectRowAtIndexPath ??编辑时,`UITableView` 没有调用 didSelectRowAtIndexPath ??
【发布时间】:2011-03-04 02:47:32
【问题描述】:

我有一个UITableViewController,用户应该可以在其中编辑项目。为了启用编辑,我使用了这个:

self.navigationItem.rightBarButtonItem = self.editButtonItem;

对于不知道 self.editButtonItem 来自哪里的人来说,它是 SDK 预定义的。

所以,现在当我按下任何项目时,都会调用此方法:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

但是当我点击编辑按钮并且编辑处于活动状态时,似乎没有调用此方法。

知道我错过了什么吗?

这是与编辑相关的其余代码:

// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    return UITableViewCellEditingStyleNone;
}


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

感谢您的帮助。

mcb

【问题讨论】:

    标签: iphone uitableview


    【解决方案1】:

    简答(Swift 语言):

    @IBOutlet private weak var tableView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        tableView.allowsSelectionDuringEditing = true // Magic happens here
    }
    

    【讨论】:

      【解决方案2】:

      您必须将 UITableView 的allowSelectionDuringEditing 属性设置为true。

      参考:UITableView - allowsSelectionDuringEditing

      【讨论】:

      • 这正是我所缺少的,谢谢
      • 神奇答案.. _superTableView.allowsSelectionDuringEditing = true;和 _superTableView.allowsSelection = true;谢谢!!
      • 四年过去了,你还在拯救生命!
      • 即使在 10 年后@thomax。
      【解决方案3】:

      我以其他方式解决了这个问题。我的编辑按钮在我自己的导航栏上,这就是它没有被调用的原因。在我将 VC 嵌入到 Navigation Controller 中并将新的 Edit 按钮放入其 Navigation Item 后,Edit 按钮开始被调用。

      【讨论】:

        【解决方案4】:

        但是当我点击编辑按钮并且编辑处于活动状态时,似乎没有调用此方法。

        知道我错过了什么吗?

        是的。您希望在点击导航栏中的按钮时调用tableView:didSelectRowAtIndexPath:。这永远不会发生。该方法仅在选择行时调用。

        点击编辑按钮时,您需要将表格置于编辑模式。在您的视图控制器中:

        - (void)setEditing:(BOOL)editing animated:(BOOL)animated
        {
            [super setEditing:editing animated:animated]; // must be called first according to Apple docs
        
            [self.tableView setEditing:editing animated:animated];
        }
        

        然后通过添加tableView:commitEditingStyle:forRowAtIndexPath: 方法来处理编辑事件。

        【讨论】:

        • 他在问为什么在他编辑时没有调用 didSelectRow,他没想到当他点击编辑时会发生这种情况
        • @Daniel 如果你仔细观察,你会发现我回答了问题。
        【解决方案5】:

        点击编辑按钮时是否设置self.editing = YES

        在编辑模式下,这个方法被调用,而不是当你点击一行时调用 didSelectRow:

        tableView:commitEditingStyle:forRowAtIndexPath:.
        

        【讨论】:

        • 我不确定您的声明是否属实,当您实际删除或编辑单元格时,commitediting 会被调用...阅读我的答案,这就是在编辑时获取 didSelectRow 回调的方法
        • 嗨,你当然是完全正确的!我没注意:/对不起。
        猜你喜欢
        • 2023-04-01
        • 2015-05-17
        • 1970-01-01
        • 1970-01-01
        • 2011-01-07
        • 2015-06-05
        • 2015-01-20
        • 1970-01-01
        相关资源
        最近更新 更多