简体   繁体   English

表格视图单元格内的嵌套集合视图 swift iOS

[英]Nested Collection View inside of table view cell swift iOS

I am nesting a collection view inside of a table view cell.我在表视图单元格内嵌套了一个collection view I need to implement a screen similar to that of the app store of netflix.我需要实现一个类似于netflix应用商店的屏幕。 Where you have categories such as您有类别的地方,例如在此处输入图片说明

I would need to load the data from a JSON, for instance - "Popular" may have 11 videos, "Action" May have 3 videos and "Adventures" may have 20. So inside each table view cell, that specific collection view would load a different number of collection view cells depending on how many videos are in each category (Popular, Action, Adventure), which will be displayed within its own respective horizontal collection view.我需要从 JSON 加载数据,例如 - “Popular”可能有 11 个视频,“Action”可能有 3 个视频,“Adventures”可能有 20 个。所以在每个表视图单元格中,特定的集合视图将加载不同数量的集合视图单元格取决于每个类别(热门、动作、冒险)中的视频数量,它们将显示在其各自的水平集合视图中。

So far with my code i display each category title in the table view header.到目前为止,我的代码在表视图标题中显示每个类别标题。 But within that category, inside the collection view numberOfItemsInSection and cellForItemAt , i can't find a way to load each collection view interdependently.但是在该类别中,在集合视图numberOfItemsInSectioncellForItemAt 中,我找不到一种方法来相互依赖地加载每个集合视图。 For example, collection view 1 that relates to Popular, should load 11 cells, Collection View 2 which relates to Action should load 3 cells.例如,与流行相关的集合视图 1应加载 11 个单元格,与操作相关的集合视图 2应加载 3 个单元格。 Below is my code so far以下是我到目前为止的代码

My TABLE VIEW METHODS WHICH ARE INSIDE OF VIEW CONTROLLER我的表视图方法位于视图控制器内部

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

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

    guard let cell = tableView.dequeueReusableCell(withIdentifier: "categoriesCell") as? CategoriesTableViewCell else { return UITableViewCell() }

    return cell
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

    return arrayOfCategoryObjects[section].title
}

func numberOfSections(in tableView: UITableView) -> Int {
    return arrayOfCategoryObjects.count
}

 func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

    if let cell = cell as? CategoriesTableViewCell {


        cell.moviesCollectionView.dataSource = self
        cell.moviesCollectionView.delegate = self

        cell.moviesCollectionView.reloadData()

    }
}

MY TABLE VIEW CELL我的表视图单元格

class CategoriesTableViewCell: UITableViewCell {

@IBOutlet weak var moviesCollectionView: UICollectionView!

}

MY COLLECTION VIEW DELEGATES WHICH ARE ALSO INSIDE THE VIEW CONTROLLER我的集合视图代理也位于视图控制器中

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

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
   // print(arrayOfCategoryObjects[section].movies[section])

    return arrayOfCategoryObjects[section].movies.count

  }

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


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

        cell.movieTitleLbl.text = arrayOfCategoryObjects[indexPath.item].movies[indexPath.item]

        return cell

  }

MY COLLECTION VIEW CELL我的收藏查看单元格

class MoviesCollectionViewCell: UICollectionViewCell {

@IBOutlet weak var movieTitleLbl: UILabel!

}

You need to set a tag for your UICollectionView as parent UITableViewCell 's indexPath.section :你需要为你的UICollectionView设置一个标签作为父UITableViewCellindexPath.section

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

    if let cell = cell as? CategoriesTableViewCell {

        cell.moviesCollectionView.dataSource = self
        cell.moviesCollectionView.delegate = self
        cell.moviesCollectionView.tag = indexPath.section
        cell.moviesCollectionView.reloadData()

    }
}

and then in your UICollectionView's delegate:然后在你的 UICollectionView 的委托中:

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

    return arrayOfCategoryObjects[collectionView.tag].movies.count

  }

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

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

    cell.movieTitleLbl.text = arrayOfCategoryObjects[collectionView.tag].movies[indexPath.item]

    return cell

  }

Hope this helps!希望这可以帮助!

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

相关问题 iOS Swift:自动调整大小的集合视图嵌套在表格视图单元格中 - iOS swift: autoresize collection view nested inside a table view cell 单元格表视图Swift iOS中的按钮 - Button inside Cell Table View Swift iOS 在表格视图单元格中添加集合视图 swift 3 - Adding a collection view inside a table view cell swift 3 表视图单元格内的集合视图 - Collection View inside Table View Cell 如何使用集合视图单元格Swift iOS实现网格视图 - How to achieve Grid view using collection view cell swift ios 将数据从集合视图单元内的表视图单元传输到另一个集合视图单元(带有图片!) - Transfer data from table view cell inside a collection view cell, to another collection view cell [with picture!] iOS(Swift):对于不可见的单元格,集合视图indexPath(for:cell)为nil - iOS (Swift): Collection View indexPath(for: cell) is nil for non visible cell 如何通过快速点击集合视图的单元格在表视图中重新加载数据 - How to reload data in table view by tapping the cell of collection view in swift 如何在Swift 3中实现表格视图单元格嵌入集合视图? - How to implement a table view cell embed collection view in swift 3? 如何在iOS的Collection View Cell内停止捕获会话 - How to stop capture session when it is inside the Collection View Cell in iOS , Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM