Swift 泛型使用1
2020-03-31 本文已影响0人
LovelyYilia
import UIKit
protocol CellConfigurable{
associatedtype T
func setCell<T>(with data:T) ;
}
class GenericCollectionViewController<C,T:UICollectionViewCell>: 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<C,T:UICollectionViewCell>: 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)
}
}