我正在关注这个著名的tutorial
而是想在另一个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
的冗余一致性。这是可以理解的,因为我已经定义了两次。如何在此处指定内部和外部collectionView的委托方法之间的区别?
最佳答案
确保存在冗余,因为这
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)
}
}
关于ios - 如何实现嵌套集合 View ?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50769718/