UITableView 滑动删除问题(使用单独的数据源对象时)

UITableView Swipe to Delete Issue (when using separate datasource object)

我有一个问题,我的 table 视图在单元格滑动时不显示删除按钮。

我创建了一个符合 UITableViewDataSource 协议的单独数据源 class。我有一个 UITableViewController,我正在使用 UIPopoverPresentationController 进行演示。当然,在展示它之前,我用它需要的所有信息设置了数据源,并确保它实现了:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}    

在我的 UITableViewController subclass 中,我也肯定会同时实现:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // do something (this doesn't get hit yet anyway, as delete button does not appear)
    }
}

问题似乎是 tableView:editingStyleForRowAtIndexPath: 方法从未被调用过。但是,如果我绕过单独的数据源 class 并简单地在我的 UITableViewController 子 class 中实现数据源,我注意到此方法 does 被调用.

我更愿意弄清楚为什么我使用单独的数据源 class 会给我滑动删除带来问题,而不是放弃这个 class(我是在我的整个应用程序中使用非 editable table 视图),而是选择将数据源完全保留在 UITableViewController subclass 中。非常感谢任何关于在哪里查看的指示!

您需要在 .m 文件中添加以下 tableView 数据源方法。

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;