[英]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.
但是在该类别中,在集合视图numberOfItemsInSection和cellForItemAt 中,我找不到一种方法来相互依赖地加载每个集合视图。 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
设置一个标签作为父UITableViewCell
的indexPath.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.