如何重用Collection View? -Swift

时间:2017-08-21 22:22:35

标签: ios swift uicollectionview reusability

在我正在尝试构建的应用程序中,我需要大量UICollectionView s(大约10)。我决定在不使用 Storyboards 的情况下制作collectionView(即完全在代码中)。 Storyboards 使事情复杂化(对于许多collectionViews)。

以下是代码:

I)

   override func viewDidLoad() {

    super.viewDidLoad()
        //Create the collection View      

    let frame =                  CGRect(origin: CGPoint.zero , size: CGSize(width: self.view.frame.width , height: 50))
    let layout =                 UICollectionViewFlowLayout()
     collectionView1 =           UICollectionView(frame: frame, collectionViewLayout: layout)
    collectionView1.dataSource = self
    collectionView1.delegate =   self
    collectionView1.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cellIdentifier")
    collectionView1.backgroundColor = UIColor.red
    view.addSubview(collectionView1) }

II)

 // TheData Source and Delegate 

extension ViewController : UICollectionViewDataSource , UICollectionViewDelegateFlowLayout{


public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

 return 5

}

public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {


        let cell =              collectionView.dequeueReusableCell(withReuseIdentifier: "cellIdentifier", for: indexPath)
        cell.backgroundColor = UIColor.darkGray
        return cell

}


func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    return CGSize(width: 50, height: 50)
}
 }

这将在View Controller中创建一个collectionView,但如果我不得不再创建十个这样的话,我将不得不重写上述代码10次。

所以我尝试创建一个单独的MyCollectionView类,其中包含将collectionView添加到ViewController所需的基本实现,以便  我在视图控制器中所做的一切就像

一样简单
 override func viewDidLoad() {

super.viewDidLoad()

 let shoesCollection = MYCollectionView(frame : ...)
 self.view.addSubview(shoesCollection)   

 let foodCollection = MYCollectionView(frame : ...)
 self.view.addSubview(foodCollection)

 let carCollection = MYCollectionView(frame : ...)
 self.view.addSubview(carCollection)   

 }

或类似的东西。但是我没有成功。 我该怎么办呢?谢谢!

1 个答案:

答案 0 :(得分:2)

我会举一个简单的例子,虽然我不确定这是不是你想要的。同样,它完全取决于您正在实施的设计,但这可能是其中一个想法。

这个想法是

  1. 创建一个集合视图,用于填充要添加的10个集合视图。

  2. 创建一个包含您想要重复的集合视图的集合视图单元格。

  3. 将不同的数据(食物,鞋子,汽车等)输入集合视图单元格(MYCollectionView)。

  4. 将每个数据都提供给单元格中的集合视图,以填充各个数据。

  5. <强>的ViewController

    class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout  {
        lazy var collectionView: UICollectionView = {
            let layout = UICollectionViewFlowLayout()
            layout.minimumInteritemSpacing = 0
            layout.minimumLineSpacing = 10
            layout.scrollDirection = .vertical
    
            let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
            collectionView.delegate = self
            collectionView.dataSource = self
            collectionView.register(MYCollectionView.self, forCellWithReuseIdentifier: NSStringFromClass(MYCollectionView.self))
            collectionView.backgroundColor = .clear
            return collectionView
        }()
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
            view.addSubview(collectionView)
        }
    
        override func viewWillLayoutSubviews() {
            super.viewWillLayoutSubviews()
            collectionView.frame = view.bounds
        }
    
        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return 10
        }
    
        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: NSStringFromClass(MYCollectionView.self), for: indexPath) as! MYCollectionView
            return cell
        }
    
    
        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
            return CGSize(width: view.bounds.width, height: 100)
        }
    }
    

    <强> MYViewController

    class MYCollectionView: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
        lazy var collectionView: UICollectionView = {
            let layout = UICollectionViewFlowLayout()
            layout.scrollDirection = .horizontal
    
            let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
            collectionView.dataSource = self
            collectionView.delegate =   self
            collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cellIdentifier")
            collectionView.backgroundColor = UIColor.red
            return collectionView
        }()
    
        override init(frame: CGRect) {
            super.init(frame: frame)
    
            contentView.addSubview(collectionView)
        }
    
        override func layoutSubviews() {
            super.layoutSubviews()
            collectionView.frame = bounds
        }
    
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    
        public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return 15
        }
    
        public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellIdentifier", for: indexPath)
            cell.backgroundColor = UIColor.darkGray
            return cell
    
        }
    
    
        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
            return CGSize(width: bounds.height, height: bounds.height)
        }
    
        func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
            print("Clicked")
        }
    }
    

    更新: 由于问题只是关于重用集合视图,所以我并没有将数据一直传递给重用的集合视图。假设在加载视图控制器时你有数据,比如说鞋子,食物和汽车的数组,而且每个数组组件都是数组。

    例如,

    let shoes = [...] //array of shoes
    let foods = [...] //array of foods
    let cars = [...] //array of cars
    let data = [shoes, foods, cars]
    

    现在您的数据数组有3个组件,这将决定您在问题中创建的集合视图的数量。所以在我的示例代码中的视图控制器中,

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return data.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: NSStringFromClass(MYCollectionView.self), for: indexPath) as! MYCollectionView
        let components = data[indexPath.item]
        cell.data = components
        return cell
    }
    

    在MYCollectionView中,你应该有一个变量data。

    var data:[WhateverYourDataModel] = [] {
        didSet {
            collectionView.releadData()
        }
    }
    
    public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return data.count
    }
    
    public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellIdentifier", for: indexPath)
        cell.backgroundColor = UIColor.darkGray
    
        //let component = data[indexPath.item]
        //You should create a custom collection view cell to display whatever you want to display with the data, component. 
    
        return cell
    }
    

    为了使其干净,您的数据对象应具有通用的基本模型,以便您可以将它们从视图控制器一直传递到MYCollectionView中的集合视图。

    enter image description here