Tableview中的CollectionView,数据源错误

Posted

技术标签:

【中文标题】Tableview中的CollectionView,数据源错误【英文标题】:CollectionView inside Tableview, error with datasource 【发布时间】:2017-03-01 12:53:08 【问题描述】:

我有一个 uiviewcontroller,我在其中放置了一个带有自定义单元格的 uitableview。这些单元格中的每一个都有一个 uicollectionview,具有水平滚动功能。

我正在以编程方式工作,因此 tableviewcell 是 collectionview 的委托/数据源。

我的数据源结构,是 arraysarray

我的 tableview,对于每个 tableviewcell,都有一个 tableview 部分。Tableview 数据源方法使用 array 没有问题。我能够正确设置表格视图。 在这个单元格内,我想显示一个带有水平滚动的集合视图。

我遇到的问题是我无法使用 arrays 将数据源正确分配给 collectionview。发生的情况是当我加载表格视图并向下滚动时,我看到重复的记录,因此例如前 3 行正确显示(就数据和项目数而言),但从第 4 行开始,例如,数据是重复,所以我再次看到前 3 行的记录,当然这不必发生,因为如果我在第 4 行滚动(第 4 行的 collectionview)应用程序由于索引问题而崩溃范围。

这很容易理解:假设我在第一行有 10 个单元格,在第 4 行/或部分有 5 个单元格,如您所愿...滚动第 4 行的集合视图将导致崩溃。这是因为错误的数据实际上与第一行相同:相反,我只有 5 个单元格要渲染...

我使用的技术非常简单:为每个 tableviewcell 的标签提供实际的 indexpath.section。然后在collectionview中,使用这个标签循环遍历array,然后indexpath.item得到正确的array

现在让我们看一下代码:

表格视图

func numberOfSections(in tableView: UITableView) -> Int 
    return DataManager.shared.datasource.count

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    return 1

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! DiarioTableViewCell
    cell.tag = indexPath.section

    return cell

TableviewCell

override init(style: UITableViewCellStyle, reuseIdentifier: String?) 
    super.init(style: .default, reuseIdentifier: "cell")

    collectionView.register(DiarioCVCell.self, forCellWithReuseIdentifier: "cellCV")
    collectionView.delegate = self
    collectionView.dataSource = self

    DataManager.shared.istanzaCV = self.collectionView

    addSubview(tableCellBG)
    addSubview(collectionView)
    setConstraints()

CollectionView

func numberOfSections(in collectionView: UICollectionView) -> Int 
    return 1


func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int 

    let tvCell = collectionView.superview as! DiarioTableViewCell
    return DataManager.shared.datasource[tvCell.tag].count



func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellCV", for: indexPath) as! DiarioCVCell

    let celltag = (collectionView.superview as! DiarioTableViewCell).tag

    cell.datasource = DataManager.shared.datasource[celltag][indexPath.item]

    return cell

请注意,我已经阅读了所有可能相关的线程,甚至 ashfurrow 的文章,以及在堆栈上,但我无法找到解决方案。 感谢您的帮助!

【问题讨论】:

【参考方案1】:

当单元格显示时,尝试重新加载您的收藏视图。单元被重复使用以节省内存,因此它们只创建一次 - 您在初始化时设置所有数据并且它会永远保留在那里。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! DiarioTableViewCell
    cell.tag = indexPath.section
    cell.collectionView.reloadData()
    cell.collectionView.collectionViewLayout.invalidateLayout() //just to be sure, maybe it's not necessary

    return cell

【讨论】:

所以基本上错误是由于缺少重新加载始终具有相同内容的集合视图引起的?我被困在这个问题上 3 周,我不敢相信这是这么简单。非常感谢 Michał Kwiecień ! @MichałKwiecień,你能帮我看看我的问题吗兄弟***.com/questions/45074376/…? cell.collectionView.collectionViewLayout.invalidateLayout() 帮助我修复了类似的崩溃。谢谢大佬。 我确实花了两天时间寻找解决方案。我以两种不同的方式重写了我的文件 3 次。然后,事实证明我只需要在该死的 cellforrowat 中重新加载数据。非常爱你

以上是关于Tableview中的CollectionView,数据源错误的主要内容,如果未能解决你的问题,请参考以下文章

单击tableview单元中的Collectionview时如何找到tableview的索引路径

检测同一控制器中的 TableView 和 CollectionView 滚动

多个tableView单元格中的多个collectionView

一个 tableView 单元中的两个 collectionView。 collectionViews 没有出现

Tableview中的CollectionView,数据源错误

从嵌入在 tableView 中的 collectionView 显示 xib