![](/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.