ios - 如何实现嵌套集合视图?
问题描述
我正在关注这个著名的教程 ,并希望在另一个 collectionView 中实现一个 collectionView。我想我的 ViewController.swift 中几乎有这个:
import UIKit
class ViewController: UIViewController, UICollectionViewDelegate,UICollectionViewDataSource {
let model: [[UIColor]] = generateRandomData()
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return model.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "outerCell", for: indexPath) as! OuterCollectionViewCell
return cell
}
func collectionView(_ collectionView: UICollectionView,
willDisplay cell: UICollectionViewCell,
forItemAt indexPath: IndexPath){
guard let outerCollectionViewCell = cell as? OuterCollectionViewCell else { return }
outerCollectionViewCell.setCollectionViewDataSourceDelegate(dataSourceDelegate: self, forRow: indexPath.row)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(collectionView: UICollectionView,
numberOfItemsInSection section: Int) -> Int {
return model[collectionView.tag].count
}
func collectionView(collectionView: UICollectionView,
cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell",
forIndexPath: indexPath)
cell.backgroundColor = model[collectionView.tag][indexPath.item]
return cell
}
}
然后:
class OuterCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var innerCollectionView: UICollectionView!
func setCollectionViewDataSourceDelegate
<D: UICollectionViewDataSource & UICollectionViewDelegate>
(dataSourceDelegate: D, forRow row: Int) {
innerCollectionView.delegate = dataSourceDelegate
innerCollectionView.dataSource = dataSourceDelegate
innerCollectionView.tag = row
innerCollectionView.reloadData()
}
}
然而 Xcode 很生气:ViewController
协议UICollectionViewDataSource
和UICollectionViewDelegate
. 这是可以理解的,因为我定义了两次..
如何在这里指定内部和外部 collectionViews 的委托方法之间的区别?
解决方案
当然有冗余,因为这
1-
class ViewController: UIViewController, UICollectionViewDelegate,UICollectionViewDataSource
有了这个
extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource {
对于这些协议,VC 应该只符合一次
2-
您只需要实现每个方法的一个副本并检查其中的 collectionView 的名称
func collectionView(collectionView: UICollectionView,
numberOfItemsInSection section: Int) -> Int {
if collectionView == mainCollectionView {
return model.count // this is the VC collection
}
else {
return model[collectionView.tag].count // cells collection
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView == mainCollectionView {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "outerCell", for: indexPath) as! OuterCollectionViewCell
return cell
}
else {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell",forIndexPath: indexPath)
cell.backgroundColor = model[collectionView.tag][indexPath.item]
return cell
}
}
func collectionView(_ collectionView: UICollectionView,
willDisplay cell: UICollectionViewCell,
forItemAt indexPath: IndexPath){
if collectionView == mainCollectionView {
guard let outerCollectionViewCell = cell as? OuterCollectionViewCell else { return }
outerCollectionViewCell.setCollectionViewDataSourceDelegate(dataSourceDelegate: self, forRow: indexPath.row)
}
}
推荐阅读
- javascript - Recharts BarChartStackedBySign - 是否可以在 Y 轴两侧显示相同的实体
- swift - NSRegularExpression 在 CRLF 之后不匹配任何内容。为什么?
- android - 使用 Firebase 身份验证的正确方法是什么?
- javascript - 循环 JSON 对象并将唯一的 JSON 项分配给给定的变量
- python-3.x - 为什么使用 Gaussian NB 分类器查找测试数据的分数时得到 0.0 的分数?
- gcc - CMAKE 中的 net-snmp 标志
- java - 我的类扩展 OutputStream 的 GZIPOutputStream 问题
- excel - 修剪不同工作表VBA中行和列中的单元格值
- c# - 使用 Selenium 在私有模式下打开 Internet Explorer
- android - 具有动态 applicationId 的 Firebase