简体   繁体   English

无法在嵌套的CollectionView中获取TableViewCell的indexPath

[英]Can’t get indexPath of TableViewCell in nestled CollectionView

I've got a tableView with collectionViews nestled in each tableViewCell. 我有一个tableView与每个tableViewCell中嵌套的collectionViews。 Each tableViewCell is a movie genre and the collectionViewCells would be the movies. 每个tableViewCell是电影类型,collectionViewCells是电影。 I'm trying to tell the collectionView which TableView indexPath it is so it can display the correct data. 我试图告诉collectionView哪个TableView indexPath,以便它可以显示正确的数据。

The problem I'm having is as you scroll through the collectionView (and TableView) the numbers change (I've got a label in the collectionView cell displaying the TableView indexPath.row). 我遇到的问题是,当您滚动浏览collectionView(和TableView)时,数字会发生变化(我在collectionView单元格中有一个标签,显示TableView indexPath.row)。 Also if I scroll to the end of a collectionView in the first tableView cell, the collectionViewCell in the 4th or so tableViewCell will also be scrolled to the end, as if they are 'linked'. 同样,如果我在第一个tableView单元格中滚动到collectionView的末尾,则第4个左右的tableViewCell也会滚动到末尾,就像它们被“链接”一样。

To get the nestled layout I'm following this tut https://youtu.be/4XnXHov2lQU I'd initially setup the collectionView from the TableViewCell Class but as explained in the video this doesn't conform to MVC. 为了获得嵌套的布局,我遵循此教程https://youtu.be/4XnXHov2lQU,我最初是通过TableViewCell类设置collectionView的,但是如视频中所述,这不符合MVC。

To get the tableViewIndexPath I just set a variable in the TableView cellForRowAt then set the label in the collectionView to this. 要获取tableViewIndexPath我只需在TableView的cellForRowAt设置一个变量,然后在collectionView中将标签设置为此。

What would be the correct way to do this as I've seen other questions and answered posted on this but pretty much all give the same problem. 这样做的正确方法是什么,因为我已经看到其他问题并对此进行了回答,但是几乎所有问题都存在相同的问题。

EDIT - TableViewController.swift 编辑 -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 TableViewCellClass.swift

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

    var indexPathForCell: IndexPath?

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

Your question clearly has a lack of code, but from your description, I suppose that you have a problem with cells reusing. 您的问题显然缺少代码,但是从您的描述来看,我想您在单元重用方面存在问题。

Let's say, that you scrolled UICollectionView at the first cell to the right. 假设您将UICollectionView滚动到右侧的第一个单元格。 If after that, you start to scroll you UITableView down, your UITableViewCells will be recused. 如果在那之后,您开始向下滚动UITableView ,则将撤消UITableViewCells So your 4th UICollectionView will be in fact, the first cell that had been reused. 因此,您的4th UICollectionView实际上将是已被重用的第一个单元格。 Reused means that it is the very same cell. Reused意味着它是同一单元格。 And since you scroll the first cell to the right, its CollectionView has the same contentOffset value and it looks like it was scrolled. 而且,由于您将第一个单元格向右滚动,因此其CollectionView具有相同的contentOffset值,并且看起来像在滚动一样。

Every reusable cell has a method prepateForReuse() that can be overridden. 每个可重复使用的单元格都有一个可以覆盖的prepateForReuse()方法。 In that method, you can reset all your cell parameters to default values. 通过这种方法,您可以将所有单元格参数重置为默认值。 For example, I'll assume that, at your table view cell class, you have a collectionView object. 例如,我假设在表视图单元格类中有一个collectionView对象。 In that case, you need to do next 在这种情况下,您需要下一步

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

After that, every reused cell will be automatically returned to its initial position. 之后,每个重复使用的单元格将自动返回其初始位置。 Also, when you'll scroll you table view back to the top, the first UICollectionView will be at the initial position as well. 另外,当您将表格视图滚动回到顶部时,第一个UICollectionView也将位于初始位置。

In your tableview cell class add extension of collectionview delegate and data source, define your layout and configure collectionview cells. 在tableview单元类中,添加collectionview委托和数据源的扩展,定义布局并配置collectionview单元。

In your view controller class go to cellForRowAIndex and call the method of tableview cells class like configureCollectionView(at section: indexPath.row) // to capture the row index 在视图控制器类中,转到cellForRowAIndex并调用tableview单元格类的方法,例如configureCollectionView(at section:indexPath.row)//捕获行索引

In your tableview class make sure you define configure method that sets the collectionview delegate, datasource and reloads data 确保在tableview类中定义了configure方法,该方法设置collectionview委托,数据源并重新加载数据

Doing this will allow you to keep the scroll positions of each row to be scrolled in a way that doesn't seemed linked. 这样做可以使每行的滚动位置以似乎没有链接的方式滚动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过语音获取当前所选tableviewcell的IndexPath? - How can I get IndexPath of currently selected tableviewcell by voice over? 如何获取tableviewcell内的indexpath uitextfield? - how to get indexpath uitextfield inside tableviewcell? 从collectionView获取indexpath,有时获取错误的indexPath - get indexpath from collectionView, sometime get the wrong indexPath 无法获得 collectionView 单元格的正确 indexPath - Cant get correct indexPath of collectionView cell 如何在TableView的CollectionView中获取图像的索引路径? - How to get a image's indexpath in CollectionView in TableView? 迅速获取collectionView中心的单元格的indexPath? - Get the indexPath of cell in the center of collectionView in swift? 如何从minimumLineSpacingForSectionAt获取IndexPath,以在collectionview中的项目之间设置不同的间距? - How can I get IndexPath from minimumLineSpacingForSectionAt to set different spacing between items in a collectionview? Swift UITest在tableviewcell内找不到collectionview - Swift UITest Can not find collectionview inside of tableviewcell 无法在TableViewCell中触发我的UIButton - Can't get my UIButton in TableViewCell to fire 我在自定义TableViewCell中有一个UICollectionView,但是我无法自动调整CollectionView的大小 - I have a UICollectionView in a custom TableViewCell but I can't auto-resize the CollectionView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM