Cod*_*ing 5 uitableview ios uicollectionview
我正在嵌套一个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()
}
}
Run Code Online (Sandbox Code Playgroud)
我的表视图单元格
class CategoriesTableViewCell: UITableViewCell {
@IBOutlet weak var moviesCollectionView: UICollectionView!
}
Run Code Online (Sandbox Code Playgroud)
我的集合视图代理也位于视图控制器中
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
}
Run Code Online (Sandbox Code Playgroud)
我的收藏查看单元格
class MoviesCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var movieTitleLbl: UILabel!
}
Run Code Online (Sandbox Code Playgroud)
您需要为您的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()
}
}
Run Code Online (Sandbox Code Playgroud)
然后在你的 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
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!
| 归档时间: |
|
| 查看次数: |
2691 次 |
| 最近记录: |