ios - 使用 TrailingSwipeActionsConfigurationForRowAt 删除行时奇怪的缺少其他行问题

标签 ios swift uitableview

我正在尝试使用 TrailingSwipeActionsConfigurationForRowAt 函数从 UITableView 中删除行。

该行被删除并消失。这部分还可以。

但是接下来出现在屏幕上的行或者当我向下滑动与已删除的行相同的量时甚至不会加载到 tableView 上。

![img1] https://poirot.deus4.com/photo_2019-06-12_16-44-01.jpg

![img2] https://poirot.deus4.com/photo_2019-06-12_16-43-56.jpg

![img3] https://poirot.deus4.com/photo_2019-06-12_16-43-49.jpg

![img4] https://poirot.deus4.com/photo_2019-06-12_16-43-38.jpg

[视频] https://poirot.deus4.com/RPReplay_Final1560345600.mp4

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return filteredProducts.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: .cellID, for: indexPath) as! ProductTableCell

        cell.backgroundColor = .red
        return cell
}

override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {

        let hide = UIContextualAction(style: .destructive, title: "Hide") { action, view, completion in

            self.filteredProducts.remove(at: indexPath.row)
            tableView.deleteRows(at: [indexPath], with: .automatic)

            completion(true)
        }

        hide.image = #imageLiteral(resourceName: "hdie_product")
        hide.backgroundColor = .midGrey

        let conf = UISwipeActionsConfiguration(actions: [hide])
        return conf
}

最佳答案

您展示的代码显然有效,因此问题出在其他地方。
如果没有其他代码,我怀疑问题是因为 TableView 单元格被重复使用:当您滚动 TableView 时,某些单元格会滚动到 View 之外并且可以重复使用。滚动的单元格要么是重复使用的单元格,要么是新的单元格。
无论如何,必须使用表格 View 数据源函数 tableView(_:cellForRowAt:) 来配置显示的单元格。
我觉得您没有在 tableView(_:cellForRowAt:) 中配置单元格。
如果是这样,重复使用的单元格看起来就像滚动出来时一样,但新单元格只是空白。 因此,我建议检查您是否确实正确配置了 tableView(_:cellForRowAt:) 中的所有单元格。

关于ios - 使用 TrailingSwipeActionsConfigurationForRowAt 删除行时奇怪的缺少其他行问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56563828/

相关文章:

iphone - 在两个 View 之间翻转时如何修复 iOS 泄漏

swift - 当字符串存在时获取 nil

ios - 使用 NSFetchedResultsController 在 TableView 上显示的元素列表与底层 CoreData 实体不匹配

uitableview - 按下按钮时 UITableView 标题 View 的动画显示(快速)

ios - 如何保护我的 iOS 应用程序不被破解?

iphone - EventKit - 添加带有 2 个警报的 EKEvent 时应用程序卡住 (iOS 5)

ios - 为什么 UIKit 不喜欢 Swift 3 的可选项?

ios - 我可以使用 Swift 获取所有现有的通知中心消息吗?

ios - 重新加载 UITableView 的内容

ios - 在 UITableViewCell 不同标识符之间共享代码