ios - Tableview 中的 CollectionView,数据源错误

标签 ios arrays uitableview swift3 uicollectionview

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

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

我的数据源结构是数组数组

对于每个 tableviewcell,我的 tableview 都有一个 tableview 部分。Tableview 数据源方法使用 array 可以毫无问题地工作。我能够正确设置表格 View 。 在这个单元格内,我想显示一个水平滚动的 Collection View 。

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

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

我使用的技术非常简单:为每个 tableviewcell 的标签提供实际的 indexpath.section。然后在 collectionview 中,使用此标记循环访问 array,然后使用 indexpath.item 获取正确的array

现在让我们看一下代码:

表格 View

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
}

表格 View 单元格

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()
}

Collection View

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 的文章,以及此处的堆栈,但我无法找到解决方案。 感谢您的帮助!

最佳答案

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

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
}

关于ios - Tableview 中的 CollectionView,数据源错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42532285/

相关文章:

ios - 自定义对象创建后为空

ios - 如何查看 NSString 是否以某个其他字符串开头?

iphone - iPhone中的UITableViewController类未调用touchesBegan方法

iOS:自定义 TableView 单元来模仿音乐播放器

ios - 无法使用 .xib 文件在我的自定义单元格中添加任何内容?

android - 来自 SVG 的 Android 和 iOS Assets

ios - 核心数据/Swift 3 - 零值被随机添加到核心数据 NSManagedObject

python - 如果条件为真则写入数组

c++ - 初始化扩展数组元素

java - 如何制作 Jbutton 图像数组