美文网首页
重构 - Swift 下 UICollectionViewCel

重构 - Swift 下 UICollectionViewCel

作者: SoaringHeart | 来源:发表于2019-09-27 10:30 被阅读0次
🌰🌰:
ctView.register(cellType: UICTViewCellOne.self)
ctView.register(supplementaryViewType: UICTReusableViewZero.self, ofKind: UICollectionView.elementKindSectionHeader)
ctView.register(supplementaryViewType: UICTReusableViewZero.self, ofKind: UICollectionView.elementKindSectionFooter)
// 获取item
let cell: UICTViewCellOne = collectionView.dequeueReusableCell(for: UICTViewCellOne.self, indexPath: indexPath)
// 获取headerView、footerView
let view = collectionView.dequeueReusableSupplementaryView(for: UICTReusableViewZero.self, kind: kind,indexPath: indexPath);

// 源码
public extension UICollectionView{
    
    @objc static let elementKindSectionItem: String = "UICollectionView.elementKindSectionItem";
    
    /// 泛型复用register cell - Type: "类名.self" (备用默认值 T.self)
    final func register<T: UICollectionViewCell>(cellType: T.Type, forCellWithReuseIdentifier identifier: String = String(describing: T.self)){
        self.register(cellType.self, forCellWithReuseIdentifier: identifier)
    }
    
    /// 泛型复用register supplementaryView - Type: "类名.self" (备用默认值 T.self)
    final func register<T: UICollectionReusableView>(supplementaryViewType: T.Type, ofKind elementKind: String = UICollectionView.elementKindSectionHeader){
        guard elementKind.contains("KindSection") else {
            return;
        }
        let kindSuf = elementKind.components(separatedBy: "KindSection").last;
        let identifier = String(describing: T.self) + kindSuf!;
        self.register(supplementaryViewType.self, forSupplementaryViewOfKind: elementKind, withReuseIdentifier: identifier)
    }
    
    /// 泛型复用cell - cellType: "类名.self" (备用默认值 T.self)
    final func dequeueReusableCell<T: UICollectionViewCell>(for cellType: T.Type, identifier: String = String(describing: T.self), indexPath: IndexPath) -> T{
        let cell = self.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath)
        return cell as! T;
    }
    
    /// 泛型复用cell - aClass: "类名()"
    final func dequeueReusableCell<T: UICollectionViewCell>(for aClass: T, identifier: String = String(describing: T.self), indexPath: IndexPath) -> T{
        return dequeueReusableCell(for: T.self, identifier: identifier, indexPath: indexPath)
    }
    
    /// 泛型复用SupplementaryView - cellType: "类名.self" (备用默认值 T.self)
    final func dequeueReusableSupplementaryView<T: UICollectionReusableView>(for cellType: T.Type, kind: String, indexPath: IndexPath) -> T{
        let kindSuf = kind.components(separatedBy: "KindSection").last;
        let identifier = String(describing: T.self) + kindSuf!;
        let view = self.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: identifier, for: indexPath)
        view.textLabel.text = kindSuf! + "\(indexPath.section)";
        
        view.backgroundColor = kind == UICollectionView.elementKindSectionHeader ? UIColor.green : UIColor.yellow;
        return view as! T;
    }
    
    /// 泛型复用SupplementaryView - aClass: "类名()"
    final func dequeueReusableSupplementaryView<T: UICollectionReusableView>(for aClass: T, kind: String, indexPath: IndexPath) -> T{
        return dequeueReusableSupplementaryView(for: T.self, kind: kind, indexPath: indexPath)
    }
    
}

相关文章

网友评论

      本文标题:重构 - Swift 下 UICollectionViewCel

      本文链接:https://www.haomeiwen.com/subject/uinzectx.html