Swift 泛型使用1

import UIKit

 protocol CellConfigurable{
    associatedtype  T
    func setCell(with data:T) ;
}


class GenericCollectionViewController: UIViewController ,UICollectionViewDelegate,UICollectionViewDataSource where T:CellConfigurable {

    var resuseIdentifier:String="cell"
    
    var dataArray:[C]=[]

    var selectItemCallback:((_ item:C)->Void)?
    
    @IBOutlet weak var noDatalabel: UILabel!

    
   @IBOutlet weak var collectionView:UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */
    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        if self.noDatalabel != nil{
            if self.dataArray.count > 0 {
                self.noDatalabel.isHidden = true
            }else{
                self.noDatalabel.isHidden = false
            }
        }
        return self.dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: resuseIdentifier, for: indexPath) as! T

        // Configure the cell...
        cell.setCell(with: dataArray[indexPath.row])
        
        

        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        self.selectItemCallback?(self.dataArray[indexPath.item])
    }

}

class GenericCollectionBasicViewController: BasicViewController ,UICollectionViewDelegate,UICollectionViewDataSource where T:CellConfigurable {

    var resuseIdentifier:String="cell"
    
    var dataArray:[C]=[]

    var selectItemCallback:((_ item:C)->Void)?
    
    @IBOutlet weak var noDatalabel: UILabel!

    
   @IBOutlet weak var collectionView:UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */
    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        if self.noDatalabel != nil{
            if self.dataArray.count > 0 {
                self.noDatalabel.isHidden = true
            }else{
                self.noDatalabel.isHidden = false
            }
        }
        
        return self.dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: resuseIdentifier, for: indexPath) as! T

        // Configure the cell...
        cell.setCell(with: dataArray[indexPath.row])
        
        

        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        self.selectItemCallback?(self.dataArray[indexPath.item])
    }
    
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        return collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "header", for: indexPath)
    }
    
    
}

你可能感兴趣的:(Swift 泛型使用1)