美文网首页
Swift-UICollectionView扩展(快速注册cel

Swift-UICollectionView扩展(快速注册cel

作者: SK丿希望 | 来源:发表于2018-09-10 16:04 被阅读0次

    使用案例(拿TextCell做测试Cell)

    注册Cell
       collectionView.hw_registerCell(cell: TextCell.self)
    
    获取Cell

    注意: 固定添加(as TextCell) 不然会崩溃

        let cell = collectionView.hw_dequeueReusableCell(indexPath: indexPath) as CollectionViewCell
    
    注册头尾部
    // 注册头部
        collectionView.hw_registerCollectionHeaderView(reusableView: SendRedPacketHeaderView.self)
    // 注册尾部
        collectionView.hw_registerCollectionFooterView(reusableView: SendRedPacketFooterView.self)
    
    获取头尾部
    // 获取头部
        let headerView = self.collectionView.hw_dequeueCollectionHeaderView(indexPath: indexPath) as SendRedPacketHeaderView
    // 获取头部
        let footerView = self.collectionView.hw_dequeueCollectionFooterView(indexPath: indexPath) as SendRedPacketFooterView
    

    注意:不管是获取cell还是头尾部后面都是固定格式 as 你的cell/头尾部 不然会出错

    原理

    import Foundation
    import UIKit
    
    protocol UICollectionViewCellFromNib {}
    extension UICollectionViewCellFromNib {
        static var identifier: String { return "\(self)ID" }
        static var nib: UINib? { return UINib(nibName: "\(self)", bundle: nil) }
        static func hw_getNibPath() -> String? {
            return Bundle.main.path(forResource: "\(self)", ofType: "nib")
        }
    }
    
    //extension UICollectionViewCell : UICollectionViewCellFromNib{}
    extension UICollectionReusableView : UICollectionViewCellFromNib{}
    
    extension UICollectionView {
        /// 注册 cell 的方法 注意:identifier是传入的 T+ID
        func hw_registerCell<T: UICollectionViewCell>(cell: T.Type) {
            if T.hw_getNibPath() != nil {
                register(T.nib, forCellWithReuseIdentifier: T.identifier)
            } else {
                register(cell, forCellWithReuseIdentifier: T.identifier)
            }
        }
        /// 从缓存池池出队已经存在的 cell
        func hw_dequeueReusableCell<T: UICollectionViewCell>(indexPath: IndexPath) -> T {
            if T.identifier == UICollectionViewCell.identifier {
                print("获取cell 后方必须固定添加(as 你的cell名称)")
                register(UICollectionViewCell.self, forCellWithReuseIdentifier: UICollectionViewCell.identifier)
                return dequeueReusableCell(withReuseIdentifier: UICollectionViewCell.identifier, for: indexPath) as! T
            }
            /// 如果这里报错 检查你是否注册cell
            return dequeueReusableCell(withReuseIdentifier: T.identifier, for: indexPath) as! T
        }
        
        /// 注册头部
        func hw_registerCollectionHeaderView<T: UICollectionReusableView>(reusableView: T.Type) {
            // T 遵守了 RegisterCellOrNib 协议,所以通过 T 就能取出 identifier 这个属性
            if T.hw_getNibPath() != nil {
                register(T.nib, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: T.identifier)
            } else {
                register(reusableView, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: T.identifier)
            }
        }
        /// 获取可重用的头部
        func hw_dequeueCollectionHeaderView<T: UICollectionReusableView>(indexPath: IndexPath) -> T {
            if T.identifier == UICollectionReusableView.identifier {
                print("获取view 后方必须固定添加(as 你的view名称)")
                register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: UICollectionReusableView.identifier)
                return dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: UICollectionReusableView.identifier, for: indexPath) as! T
            }
            return dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: T.identifier, for: indexPath) as! T
        }
        /// 注册尾部
        func hw_registerCollectionFooterView<T: UICollectionReusableView>(reusableView: T.Type) {
            // T 遵守了 RegisterCellOrNib 协议,所以通过 T 就能取出 identifier 这个属性
            if T.hw_getNibPath() != nil {
                register(T.nib, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: T.identifier)
            } else {
                register(reusableView, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: T.identifier)
            }
        }
        /// 获取可重用的尾部
        func hw_dequeueCollectionFooterView<T: UICollectionReusableView>(indexPath: IndexPath) -> T {
            if T.identifier == UICollectionReusableView.identifier {
                print("获取view 后方必须固定添加(as 你的view名称)")
                register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: UICollectionReusableView.identifier)
                return dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionFooter, withReuseIdentifier: UICollectionReusableView.identifier, for: indexPath) as! T
            }
            return dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionFooter, withReuseIdentifier: T.identifier, for: indexPath) as! T
        }
    }
    

    相关文章

      网友评论

          本文标题:Swift-UICollectionView扩展(快速注册cel

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