swift - 连续左滑后 UITableViewCell 缺少删除控件
问题描述
我使用带有可编辑自定义样式单元格的表格视图。导航栏中有一个Edit
按钮可以切换编辑模式。我需要编辑模式来删除和重新排序行。
当我向左滑动单元格时,Delete
操作按钮会显示在该单元格的右侧。当它显示时,我按下Edit
按钮,Delete
操作按钮消失。再次按下该Edit
按钮将显示删除命令左侧和所有(!)单元格右侧的重新排序控件。到目前为止一切都符合预期。
现在(例如),当我向左滑动单元格 0,然后Delete
在单元格 0 中的操作按钮仍然可见时向左滑动单元格 1,使用Edit
按钮激活编辑模式现在显示删除命令左侧和所有单元格的重新排序控制权,除了(!) 单元格 0。
我发现,只要左擦除 2 个或更多单元格,当使用Edit
按钮启用所有单元格的编辑模式时,第一个未右滑退出编辑模式的单元格就会错过删除控件(左)。更奇怪的是……在主题单元格缺少删除控件(左)上,重新排序控件(右)显示正确!
我遵循并比较了许多教程,但没有发现错误。
我想念什么?
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
...
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
...
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
...
}
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
...
}
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
...
}
override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
...
}
// not necessary = didn't change anything, but found in many tutorials
override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)
}
@IBAction func editAction(_ sender: Any) { // that's the Edit button action
self.setEditing(!isEditing, animated: true)
self.navigationItem.leftBarButtonItem?.title = isEditing ? "Done" : "Edit"
}
最重要的是涉及的方法。不相关的方法内容被省略以保持帖子紧凑。如果认为需要,我将添加/编辑。
解决方案
嗨,我相信您的问题可以通过使用 UITableView 的预设来解决editButtonItem
。
navigationItem.leftBarButtonItem = self.editButtonItem
当执行滑动操作时,这会将按钮的状态设置为编辑模式,因此应该可以解决您遇到的问题。
推荐阅读
- xaml - Xamarin 表单从 listview 绑定到 viewmodel
- php - 如何在 Laravel 版本 8 中设置每秒的速率限制器
- javascript - React Native - 获取错误的 POST 请求 [TypeError:网络请求失败]
- javascript - 如何在最小值和最大值之间生成随机的十六进制颜色?
- android - 在密钥库 C:\Users\alan\newapp\android\app\key.jks 中找不到别名为“上传”的密钥
- c++ - 是否可以在概念中指定模板类?
- ios - SwiftUI 和 CoreData:将 FetchedResults 传递给子视图时出错(EXC_BAD_ACCESS)
- http - 整个正文的 HTTP 响应最大内容长度与标头位于同一块中
- java - 计算 REST API 测试覆盖率
- azure - 为什么 get-azureadserviceprincipal 找不到服务主体?