【发布时间】:2021-02-14 22:26:35
【问题描述】:
我在本月初发布了有关此问题的信息,但没有人回复,随着时间的流逝,我尝试自己更改它,但在阅读了大约 20 个帖子并尝试从每个帖子中实现代码之后,我仍然感到困惑。
我想要做的就是成功删除我的应用程序中的一个单元格,没有错误。
这是我在尝试删除单元格时不断遇到的错误,我已经阅读了一百万次,并尝试多次更改我的代码,但我再也受不了了。
这是我的代码:
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> [SwipeAction]? {
guard orientation == .right else { return nil }
let deleteAction = SwipeAction(style: .destructive, title: "Delete") { action, indexPath in
let documentId = self.documentsID[indexPath.row]!.docID
self.db.document("school_users/\(self.user?.uid)/events/\(documentId)").delete { (err) in
if let err = err {
print("Error deleting the docs")
} else {
print("Docs successfully deleted.")
}
}
self.documentsID.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
}
deleteAction.image = UIImage(named: "delete-icon")
return [deleteAction]
}
这个函数可以从数据库中删除文档,这很好,但它也完全使我的应用程序崩溃,我尝试将东西包装在 DispatchQueue.main 闭包、DispatchQueue.global 闭包中,但都无济于事。是的,这是一个令人尴尬的问题,但我只想弄清楚一次,不必再担心。
我还将附上我的 numberOfRowsInSection 方法,因为错误也提到了这一点。
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if events.count > 0 {
tableView.separatorStyle = .singleLine
tableView.separatorColor = .systemGreen
tableView.separatorInset = UIEdgeInsets(top: 0, left: 25, bottom: 0, right: 25)
return events.count
} else {
tableView.separatorStyle = .none
return 0
}
}
谢谢。
【问题讨论】:
标签: swift uitableview nsindexpath uitableviewrowaction