美文网首页
IGListKit的使用

IGListKit的使用

作者: 太平洋_cfd2 | 来源:发表于2021-12-31 11:55 被阅读0次
  1. IGListKit是一个CollectionView的附加使用组件库,没有依赖CollectionView,是一个独立的组件
  2. 它将collectionView的Model、Cell、Viewcontroller很好的隔开,并添加了自己独有的SectionController来管理Cell,同时用adapter管理Model、View、ViewController
  3. 上代码 ->
    1. 创建adapter(关联self, 本ViewController)
    lazy var adapter: ListAdapter = {
        let adapter = ListAdapter.init(updater: ListAdapterUpdater(), viewController: self)
        return adapter
    }()

    2. 创建collection
    let collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
//        layout.estimatedItemSize = .init(width: 1, height: 1)
        let view = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        return view
    }()
    3. 创建测试model
    var data: [GXDPostModel]?
    
    let json: [String: Any] = [
        "username": "bob",
        "image": "https://www.baidu.com/link?url=1Iq8rbcwEAwy4PvEEci0W2LsfdAxLuNuwjx0b4EkTj8qj5lMcnkPfeJVXsgMG5-miJwr_e4e7Dn93GIS8LQxvjCBnuzTNv5L1Kkr8S-FomVW4EXvpF8gK7qmdg3SYPNFsSgB9eeP2cB7C_eIuEaelOseO6GxyNk6ZWlS4ZBX2jYXkvMuh96sDkeOMq-Sd8yt2eay8HYyWpI3nmCAf0vIYx7Z5l0lIydpGycPtQgq4RvXa6r-0XR77pgxnHFjybyIpBgQFTNwUVqv6By8CUjdaYqh-l84JjOaQUBl7BIxASiZRaCQOP3PiRII7jLDZWFM9zMeX1CT70Hg4VYH6cbYR9ivxuA3TQ1u4tWenTTAUK9BouKN9KkC8jBmAjj4CIDb41rY7zU7NJ8rxHUEWA3ykGVViyT8iDBx0fxBEGXf9kK8s7gXjA-sRzwsF9FWcsmg2D4PKvbR_Lrzre5bEQgfkK1xxYmBL15njfefb4gB9smSrFO2svvEgdBYOGgD9F7N3NA3OlZyO3DaqAkWLIuprt94iyqlOu6mz7kG7RObXrsW0lKFaAQ0ZSVmgVBWzT7sidRWdREk9sOeDK9UPpu0PeNMMQug6TwQ3MwfBdDNRU9-R-T0RER2jtOiAzNc4z4fBPbxT7oBWg0c5jE7t0j6KzyNj-YYVZUWjXqPOhS-BhC&wd=&eqid=deebd7420008aa6b0000000661b0826a",
        "comment": [
            "content": "my name is bob, this is a bigboy"
        ]
    ]
    4. adapter关联collectionView和添加在self上的代理来关联        
    sectionController和model,以及collectionView的空页面
    adapter.collectionView = collectionView
    adapter.dataSource = self
    func objects(for listAdapter: ListAdapter) -> [ListDiffable] {
        return data ?? [ListDiffable]()
    }
    
    func listAdapter(_ listAdapter: ListAdapter, sectionControllerFor object: Any) -> ListSectionController {
        GXDPostSectionC()
    }
    
    func emptyView(for listAdapter: ListAdapter) -> UIView? {
        nil
    }
5. 封装一下model、sectionController、bindSectionController来解决每次需要依赖IGListKit的问题

class GXDIGListModel: NSObject, ListDiffable, Convertible {
required override init() {
    super.init()
    
}
/// 唯一标识符
func diffIdentifier() -> NSObjectProtocol {
    self
}

/// reload时,需要更新(通过比较新model与上一个model之间的区别去更新,比如是同一个的比较方法)
func isEqual(toDiffableObject object: ListDiffable?) -> Bool {
    isEqual(object)
}
}


class GXDCollectionCell: UICollectionViewCell, ListBindable {
func bindViewModel(_ viewModel: Any) {
    
}
}


class GXDSectionController<T>: ListSectionController {

var model: T?

override func sizeForItem(at index: Int) -> CGSize {
    .init(width: 1, height: 1)
}

override func didUpdate(to object: Any) {
    model = object as? T
}
}

class GXDBindSectionController: ListBindingSectionController<ListDiffable>, ListBindingSectionControllerDataSource {

override init() {
    super.init()
    
    self.dataSource = self
}

func sectionController(_ sectionController: ListBindingSectionController<ListDiffable>, viewModelsFor object: Any) -> [ListDiffable] {
    [ListDiffable]()
}

func sectionController(_ sectionController: ListBindingSectionController<ListDiffable>, cellForViewModel viewModel: Any, at index: Int) -> UICollectionViewCell & ListBindable {
    GXDCollectionCell()
}

func sectionController(_ sectionController: ListBindingSectionController<ListDiffable>, sizeForViewModel viewModel: Any, at index: Int) -> CGSize {
    .zero
}


}
    /// sectionController之间的间距通过inset控制
    override init() {
        super.init()
        
        inset = .init(top: <#T##CGFloat#>, left: <#T##CGFloat#>, bottom: <#T##CGFloat#>, right: <#T##CGFloat#>)
    }

/// 封装adapter和collectionView

class GXDAdapterViewController: GXDViewController, ListAdapterDataSource {
    
    let collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        layout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
        let view = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        view.backgroundColor = .white
        return view
    }()

    lazy var adapter: ListAdapter = {
        let adapter = ListAdapter.init(updater: ListAdapterUpdater(), viewController: self)
        adapter.dataSource = self
        return adapter
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(collectionView)
        adapter.collectionView = collectionView
        
        collectionView.snp.makeConstraints { make in
            make.top.equalTo(kNavigationbarHeight)
            make.left.right.bottom.equalToSuperview()
        }
    }
    
    func objects(for listAdapter: ListAdapter) -> [ListDiffable] {
        [ListDiffable]()
    }
    
    func listAdapter(_ listAdapter: ListAdapter, sectionControllerFor object: Any) -> ListSectionController {
        ListSectionController()
    }
    
    func emptyView(for listAdapter: ListAdapter) -> UIView? {
        nil
    }
    
}

ps: 参考链接:
https://learnku.com/articles/24068
https://jovins.cn/posts/IGListKit/

相关文章

网友评论

      本文标题:IGListKit的使用

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