uitableview – 具有破坏性样式的UIContextualAction似乎默认删除行

uitableview – 具有破坏性样式的UIContextualAction似乎默认删除行,第1张

概述我看到的问题是,当我使用.destructive创建一个UIContextualAction并在completionHandler中传递true时,似乎有一个删除行的默认 *** 作. 如果您从Xcode的模板创建一个新的Master-Detail应用程序并在MasterViewController中添加此代码… override func tableView(_ tableView: UITableVi 我看到的问题是,当我使用.destructive创建一个UIContextualAction并在completionHandler中传递true时,似乎有一个删除行的默认 *** 作.

如果您从Xcode的模板创建一个新的master-detail应用程序并在MasterVIEwController中添加此代码…

overrIDe func tableVIEw(_ tableVIEw: UItableVIEw,trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {    let testAction = UIContextualAction(style: .destructive,Title: "Test") { (_,_,completionHandler) in        print("test")        completionHandler(true)    }    return UISwipeActionsConfiguration(actions: [testAction])}

您要滑动的行将被删除.请注意,没有代码更新表视图.此外,模型不会更新,如果向上滚动导致重新加载行,它将重新出现.

在这种情况下传递false不会删除该行.或者使用.normal样式和true也不会删除该行.

默认情况下,删除行的.destructive和true结果.

谁能解释这种行为?为什么要删除行?

解决方法 根据破坏性选项的文档:

An action that deletes data or performs some type of destructive task.

完成意味着表明行动是否成功.传递真实意味着破坏性任务是成功的,因此应该删除行.

您需要在发生破坏性 *** 作时手动更新dataSource,否则会导致滚动以重新显示数据.您还需要告诉tableVIEw数据已被删除.

下面是一些显示工作示例的代码:

UIContextualAction(style: .destructive,Title: "Delete") { [weak self] (_,completion) in    if self?.canDelete(indexPath) { // We can actually delete        // remove the object from the data source        self?.myData.remove(at: indexPath.row)        // delete the row.  Without deleting the row or reloading the        // tablevIEw,the index will be off in future swipes        self?.tableVIEw?.deleteRows(at: [indexPath],with: .none)        // Let the action kNow it was a success.  In this case the         // tablevIEw will animate the cell removal with the swipe        completion(true)        } else { // We can't delete for some reason        // This resets the swipe state and nothing is removed from        // the screen visually.        completion(false)    }         }

然后我需要重新加载tablevIEw或调用deleteRows,以便在下一次滑动时正确计算indexPath.

如果我有10行并且我将第5行刷到要删除,那么除非重新加载tablevIEw或者告诉tablevIEw以某种方式删除行,否则之后的每一行都将关闭一行.

总结

以上是内存溢出为你收集整理的uitableview – 具有破坏性样式的UIContextualAction似乎默认删除行全部内容,希望文章能够帮你解决uitableview – 具有破坏性样式的UIContextualAction似乎默认删除行所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1063532.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存