首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用trailingSwipeActionsConfigurationForRowAt删除行时出现奇怪的缺少其他行的问题

使用trailingSwipeActionsConfigurationForRowAt删除行时出现奇怪的缺少其他行的问题
EN

Stack Overflow用户
提问于 2019-06-12 21:50:18
回答 2查看 300关注 0票数 0

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

该行将被删除并消失。这部分没问题。

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

(https://poirot.deus4.com/photo_2019-06-12_16-44-01.jpg)

(https://poirot.deus4.com/photo_2019-06-12_16-43-56.jpg)

(https://poirot.deus4.com/photo_2019-06-12_16-43-49.jpg)

(https://poirot.deus4.com/photo_2019-06-12_16-43-38.jpg)

video

代码语言:javascript
复制
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
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-06-13 14:23:59

问题是我没有在TablewViewCell内部调用super.prepareForReuse()

代码语言:javascript
复制
override func prepareForReuse() {
    super.prepareForReuse() // this was missing

}
票数 0
EN

Stack Overflow用户

发布于 2019-06-13 04:37:51

您展示的代码显然是有效的,因此问题出在别处。

在没有其他代码的情况下,我怀疑问题是因为表格视图单元格被重用了:当您滚动表格视图时,一些单元格滚动到视图之外,可以重复使用。滚动进入的单元格可以是重复使用的单元格,也可以是新的单元格。

在任何情况下,都必须使用表视图数据源函数tableView(_:cellForRowAt:)来配置显示的单元格。

您不在tableView(_:cellForRowAt:)中配置单元,这似乎太像我了。

如果是这样的话,重新使用的单元格在滚动出来时看起来像以前一样,但新的单元格只是空白。所以我建议检查一下你是否真的在tableView(_:cellForRowAt:)中正确地配置了所有的单元。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56563828

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档