【问题标题】:UITableView - Deleting rows with animationUITableView - 删除带有动画的行
【发布时间】:2018-10-13 20:48:18
【问题描述】:

我正在尝试使用动画从 tableview 中删除行,但是在大多数情况下它会卡住。 15 次尝试中的 1 次将导致播放此动画。这是我的删除操作的样子:

func contextualDeleteAction(forRowAtIndexPath indexPath: IndexPath) -> UIContextualAction {

    let action = UIContextualAction(style: .destructive,
                                    title: "Delete") { (contextAction: UIContextualAction, sourceView: UIView, completionHandler: (Bool) -> Void) in

                                            Model().context.delete(notesArray[indexPath.row])
                                            notesArray.remove(at: indexPath.row)
                                            self.myTableView.deleteRows(at: [indexPath], with: UITableView.RowAnimation.bottom)
                                            Model().saveItems()                             

    }
    action.title = "Delete"

    return action
}

这是刷卡时卡住的样子。

当按下删除按钮时。

我也尝试过使用 tableView.beginUpdate() 和 tableView.endUpdate() 但没有得到不同的结果。

【问题讨论】:

    标签: swift uitableview uikit


    【解决方案1】:

    问题是你忘记了你在这个方法中的主要职责:你必须调用完成处理程序!

        Model().context.delete(notesArray[indexPath.row])
        notesArray.remove(at: indexPath.row)
        self.myTableView.deleteRows(at: [indexPath], with: UITableView.RowAnimation.bottom)
        Model().saveItems()
        completionHandler(true) // <-- look!
    

    【讨论】:

      【解决方案2】:

      Swift 4 中,您可以像 iPhone 消息动画一样执行删除动画。

      使用这个:

      tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
      

      【讨论】:

      • 试图使用它,同样的事情发生了,只是语法不同
      • @MartinDichtler。您的问题不够清楚,请问您在哪里执行动画?
      • trailingSwipeActionsConfigurationForRowAt,调用上面的函数来配置action
      • 是的,我知道,@matt 能够回答我没有完成导致它没有动画的完成处理程序。
      • 我喜欢@matt 给出的每一个答案。一个励志的家伙。祝你的代码好运