ios - 滑动以从 TableView 中删除不起作用
问题描述
我只是想将“滑动删除功能”添加到我的tableView
,据我所知,我唯一需要添加的是这两个功能:
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
self.deleteWishDelegate?.deleteWish(indexPath)
}
}
但这无济于事。我不能刷卡。我注意到的一件奇怪的事情是,我的整体顶部View
有一个TransitionView
,我不知道它是从哪里来的。但是我可以点击Button
里面的 atableViewCell
所以我认为它不会阻止任何东西。
有人知道这里发生了什么吗?如果您需要更多详细信息,请告诉我。
更新:
正如评论中所建议的,我使用了另一个函数来完成工作:
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let deleteAction = UIContextualAction(style: .destructive, title: "Löschen") { _, _, completionHandler in
self.deleteWishDelegate?.deleteWish(indexPath)
completionHandler(true)
}
deleteAction.backgroundColor = .red
deleteAction.image = UIImage(systemName: "trash")
let configuration = UISwipeActionsConfiguration(actions: [deleteAction])
configuration.performsFirstActionWithFullSwipe = false
return configuration
}
还有我的 delteWsihDelegate 函数:
extension WishlistViewController: DeleteWishDelegate {
func deleteWish(_ idx: IndexPath){
// remove the wish from the user's currently selected wishlist
wishList.wishes.remove(at: idx.row)
// set the updated data as the data for the table view
theTableView.wishData.remove(at: idx.row)
self.theTableView.tableView.deleteRows(at: [idx], with: .right)
print("deleted")
}
}
现在的问题是,我每刷 20 到 30 次才有效。任何人都知道为什么会发生这种情况?
更新 2
我发现了问题所在。我也有GestureRecognizer
同样的看法。删除该功能后,该功能可以正常工作。有没有办法让两者同时工作?
解决方案
实现UITableViewDelegate
方法
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath)
可用于配置滑动操作 - 在您的情况下为删除。这是我在一个项目中使用的示例:
extension ExampleViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let deleteAction = UIContextualAction(style: .destructive,
title: "Delete") { [weak self] _, _, complete in
tableView.deleteRows(at: [indexPath], with: .automatic)
complete(true)
}
deleteAction.backgroundColor = .red
return UISwipeActionsConfiguration(actions: [deleteAction])
}
// instead of a gesture recognizer you can use this delegate method
// it will be called whenever you tap on a cell
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// whatever you gesture recognizer did
}
}
由于您在整个视图上的 GestureRecognizer 会给您带来麻烦,因此我建议您使用第三方实现或苹果公共 API 来处理关闭您的 viewController 而不是自己实现它。在这种情况下,您可以使用 viewController 的 UIModalPresentationStyle。
func startExampleViewController() {
exampleViewController.modalPresentationStyle = .formSheet
previousViewController.present(exampleViewController, animated: true)
}
推荐阅读
- microcontroller - stm32f1 实时时钟告警
- javascript - 如何从每个字符串中计算小数?
- python - 为行组填充空值
- java - 如何更改我的代码以接受用户输入
- kotlin - 当结果应该具有值或没有值时改进 applicativeNel 验证
- c# - JToken NullReferenceException
- wiremock - 场景:第一次响应成功,但第二次相同的请求返回 404 错误码
- autodesk-forge - 模型导数:获取特定对象的属性失败,出现 404
- c# - 如何在 asp.net C# 中为某些控制器操作设置默认路由?
- asynchronous - 如何在请求后使 IdentityDbContext 保持活动状态,以便服务可以使用 UserManager 完成其任务?