![](/img/trans.png)
[英]iOS swift: autoresize collection view nested inside a table view cell
[英]Nested Collection View inside of table view cell swift iOS
我在表视图单元格内嵌套了一个collection view
。 我需要实现一个类似于netflix应用商店的屏幕。 您有类别的地方,例如
我需要从 JSON 加载数据,例如 - “Popular”可能有 11 个视频,“Action”可能有 3 个视频,“Adventures”可能有 20 个。所以在每个表视图单元格中,特定的集合视图将加载不同数量的集合视图单元格取决于每个类别(热门、动作、冒险)中的视频数量,它们将显示在其各自的水平集合视图中。
到目前为止,我的代码在表视图标题中显示每个类别标题。 但是在该类别中,在集合视图numberOfItemsInSection和cellForItemAt 中,我找不到一种方法来相互依赖地加载每个集合视图。 例如,与流行相关的集合视图 1应加载 11 个单元格,与操作相关的集合视图 2应加载 3 个单元格。 以下是我到目前为止的代码
我的表视图方法位于视图控制器内部
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()
}
}
我的表视图单元格
class CategoriesTableViewCell: UITableViewCell {
@IBOutlet weak var moviesCollectionView: UICollectionView!
}
我的集合视图代理也位于视图控制器中
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
}
我的收藏查看单元格
class MoviesCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var movieTitleLbl: UILabel!
}
你需要为你的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()
}
}
然后在你的 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
}
希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.