ios - 无法在 neSTLed CollectionView 中获取 TableViewCell 的 indexPath

标签 ios swift uitableview uicollectionview

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

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

为了获得嵌套布局,我正在关注这个图 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()
    }
}

最佳答案

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

假设您在右侧的第一个单元格处滚动了 UICollectionView。如果在那之后,您开始向下滚动 UITableView,您的 UITableViewCells 将被取消。所以你的 4th UICollectionView 实际上是第一个被重用的单元格。 Reused 意味着它是同一个单元格。由于您将第一个单元格向右滚动,因此它的 CollectionView 具有相同的 contentOffset 值,看起来它被滚动了。

每个可重用单元格都有一个可以重写的方法 prepateForReuse()。在该方法中,您可以将所有单元格参数重置为默认值。例如,我假设在您的 TableView 单元格类中,您有一个 collectionView 对象。在这种情况下,您需要做下一步

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

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

关于ios - 无法在 neSTLed CollectionView 中获取 TableViewCell 的 indexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52461693/

相关文章:

objective-c - iOS:如何正确访问 UITableViewCell 的 contentView 上的自定义标签?

ios - 在 UILabel 中显示分数

ios - swift:长按手势识别器不起作用

iphone - 如何在单个 tableviewcontroller 中有主题和子主题?

ios - 进入前台时出现 UIRefreshControl 错误

iphone - 将最后一行移出部分并删除部分时出现奇怪的动画

ios - 后退按钮未出现在导航栏中

ios - 在堆栈 View 内的 Uilabels 之间创建垂直线

ios - 从函数返回之前等待 Firebase 加载

ios - 带有 NSFetchedResultController 的 TableView 在屏幕外显示损坏的单元格