1

我有一个 tableview 控制器,它是使用 diffable 数据源的拆分视图控制器中的主控,我试图找出一个问题,它无法通过滑动删除选项来运行。起初我尝试使用这一系列代码:

   override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }

    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            let context = fetchedResultsController.managedObjectContext
            context.delete(fetchedResultsController.object(at: indexPath))
            tableView.deleteRows(at: [indexPath], with: .fade)
            do {
                try context.save()
            } catch {
                // Replace this implementation with code to handle the error appropriately.
                // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
                let nserror = error as NSError
                fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
            }
        }
    }

但这并没有产生任何结果,实际上滑动实际上是从 master 移动到 detail 的。所以我接下来尝试使用:

override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
        return .delete
    }

override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
        let deleteAction = UIContextualAction(style: .destructive, title: "Delete") { (contextualAction, view, completionHandler) in
            guard let item = self.dataSource?.itemIdentifier(for: indexPath) else { return }
            self.container.viewContext.delete(item)
            self.setupSnapshot()
            completionHandler(true)
        }
        deleteAction.image = UIImage(systemName: "trash.fill")

        return UISwipeActionsConfiguration(actions: [deleteAction])
    }

再次获得相同的结果,无论我尝试什么,滑动只会产生移动到细节。在我的应用程序中移动到主服务器时,我确实收到错误消息:

Warning once only: UITableView was told to layout its visible cells and other contents without being in the view hierarchy (the table view or one of its superviews has not been added to a window). This may cause bugs by forcing views inside the table view to load and perform layout without accurate information (e.g. table view bounds, trait collection, layout margins, safe area insets, etc), and will also cause unnecessary performance overhead due to extra layout passes.

我不确定这是否会影响我正在做的事情,或者对此的解决方法是什么。这可能是问题吗?我该如何照顾它?该应用程序仍在运行,但没有滑动删除功能。我真的在这里挠头。

4

0 回答 0