【问题标题】:Can’t get indexPath of TableViewCell in nestled CollectionView无法在嵌套的 CollectionView 中获取 TableViewCell 的 indexPath
【发布时间】:2019-02-26 22:10:42
【问题描述】:

我有一个 tableView,每个 tableViewCell 中都有 collectionViews。 每个 tableViewCell 都是电影类型,而 collectionViewCells 就是电影。 我试图告诉 collectionView 它是哪个 TableView indexPath,以便它可以显示正确的数据。

我遇到的问题是,当您滚动浏览 collectionView(和 TableView)时,数字会发生变化(我在 collectionView 单元格中有一个标签,显示 TableView indexPath.row)。此外,如果我在第一个 tableView 单元格中滚动到 collectionView 的末尾,那么第 4 个左右的 tableViewCell 中的 collectionViewCell 也将滚动到末尾,就好像它们是“链接的”一样。

为了获得嵌套的布局,我正在关注这个 tut https://youtu.be/4XnXHov2lQU 我最初是从 TableViewCell 类中设置 collectionView,但正如视频中所述,这不符合 MVC。

要获得tableViewIndexPath,我只需在TableView 中设置一个变量cellForRowAt,然后将collectionView 中的标签设置为此。

什么是正确的方法,因为我已经看到了其他问题并在此发布了答案,但几乎所有问题都给出了相同的问题。

编辑 - TableViewController.swift

class TableViewController: UITableViewController {

    var tableViewIndexPath = Int()

    override func viewDidLoad() {
        super.viewDidLoad()          
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

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

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 230
    }

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

        tableViewIndexPath = indexPath.row

        cell.collectionView.dataSource = self
        cell.collectionView.reloadData()

        return cell
    }
}

extension TableViewController : UICollectionViewDataSource {

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

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10
    }


    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCell", for: indexPath)

        let title = cell.viewWithTag(4) as! UILabel
        title.text = String(tableViewIndexPath)

        return cell
    }

}

TableViewCellClass.swift

class tableViewCellClass: UITableViewCell {
    @IBOutlet weak var collectionView: UICollectionView!

    var indexPathForCell: IndexPath?

    override func prepareForReuse() {
        self.collectionView.contentOffset = .zero
        self.collectionView.reloadData()
    }
}

【问题讨论】:

    标签: ios swift uitableview uicollectionview


    【解决方案1】:

    您的问题显然缺少代码,但根据您的描述,我想您在单元格重用方面存在问题。

    假设您在右侧第一个单元格处滚动了UICollectionView。如果在那之后,您开始向下滚动UITableView,您的UITableViewCells 将被拒绝。所以你的4th UICollectionView 实际上是第一个被重用的单元格。 Reused 表示它是同一个单元格。由于您将第一个单元格滚动到右侧,它的 CollectionView 具有相同的 contentOffset 值,并且看起来像是被滚动了。

    每个可重复使用的单元格都有一个可以被覆盖的方法prepateForReuse()。在该方法中,您可以将所有单元格参数重置为默认值。例如,我假设在您的表格视图单元格类中,您有一个collectionView 对象。在这种情况下,您需要执行下一步

    override func prepareForReuse() {
        self.collectionView.contentOffset = .zero
    }
    

    之后,每个重复使用的单元格将自动返回到其初始位置。此外,当您将表格视图滚动回顶部时,第一个 UICollectionView 也将位于初始位置。

    【讨论】:

    • 是的,这基本上就是我遇到的问题。我尝试在 prepareForReuse() 中重新加载 collectionView,但它不会让用户滚动到 collection view 中的位置(原因很明显)。 (不好意思没有代码,暂时不在我的电脑上,会在里面编辑一些)
    • App Store 使用相同的布局,但仍然保持位置并且没有故障等。您知道他们是如何设置的吗?
    • 如果您想保留每个单元格的用户滚动位置,例如,您需要以某种方式将它们存储在单独的数组中。
    • 我该怎么做?
    • 这是一个完全不同的问题。你的问题是关于为什么你的新单元格被滚动好像它们是“链接的”。我试图向您解释为什么以及如何使它们最初不被滚动。
    【解决方案2】:

    在您的 tableview 单元格类中添加 collectionview 委托和数据源的扩展,定义您的布局并配置 collectionview 单元格。

    在您的视图控制器类中,转到 cellForRowAIndex 并调用 tableview 单元格类的方法,如 configureCollectionView(at section: indexPath.row) // 以捕获行索引

    在你的 tableview 类中确保你定义了 configure 方法来设置 collectionview 委托、数据源和重新加载数据

    这样做可以让您保持每行的滚动位置以一种似乎没有关联的方式滚动。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-30
      相关资源
      最近更新 更多