IGListKit使用

IGListKit使用

Jerome Xiong
Jerome Xiong

IGListKit 是 Instagram 推出的高性能列表框架,本文详细介绍其基类控制器、数据模型和 Cell 绑定的使用方式。

创建基类控制器

所有IGListKit的视图控制器都应该继承此类,减少复用

1class BaseListVC: UIViewController {
2
3    var objects: [ListDiffable] = [ListDiffable]()
4
5    lazy var collectionView: UICollectionView = {
6        let flow = UICollectionViewFlowLayout()
7        let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: flow)
8        if #available(iOS 11.0, *) {
9            collectionView.contentInsetAdjustmentBehavior = .never
10        } else {
11            automaticallyAdjustsScrollViewInsets = false
12        }
13        collectionView.backgroundColor = UIColor.groupTableViewBackground
14        return collectionView
15    }()
16    lazy var adapter: ListAdapter = {
17        let adapter = ListAdapter(updater: ListAdapterUpdater(), viewController: self)
18        return adapter
19    }()
20
21    override func viewDidLoad() {
22        super.viewDidLoad()
23        view.addSubview(collectionView)
24        adapter.collectionView = collectionView
25        adapter.dataSource = self
26    }
27
28    override func viewDidLayoutSubviews() {
29        super.viewDidLayoutSubviews()
30        collectionView.frame = view.bounds
31    }
32}
33
34extension BaseListVC : ListAdapterDataSource {
35    func objects(for listAdapter: ListAdapter) -> [ListDiffable] {
36        return objects
37    }
38
39    func listAdapter(_ listAdapter: ListAdapter, sectionControllerFor object: Any) -> ListSectionController {
40        return ListSectionController()
41    }
42
43    func emptyView(for listAdapter: ListAdapter) -> UIView? {
44        // 无数据时collectionView的展示
45        return nil
46    }
47}

创建MomentInfo模型

1class MomentInfo {
2    var id: Int = 0
3    ...
4}
5extension MomentInfo: ListDiffable {
6    func diffIdentifier() -> NSObjectProtocol {
7        // 区分是否为同一对象,可多属性叠加
8        return id as NSObjectProtocol
9    }
10
11    // 判断对象是否相同,不同(false)则刷新
12    func isEqual(toDiffableObject object: ListDiffable?) -> Bool {
13        guard self === object else { return true }
14        guard let object = object as? MomentInfo else { return false }
15        return id == object.id
16    }
17}
18
19extension MomentInfo: Equatable {
20    static func == (lhs: MomentInfo, rhs: MomentInfo) -> Bool {
21        return lhs.isEqual(toDiffableObject: rhs)
22    }
23}

子类复写

  1. 更新数据
1//info -> MomentInfo的对象
2self.objects.append(info)
3// 数据添加完成后更新
4self.adapter.performUpdates(animated: true, completion: nil)
  1. 复写绑定Section
1override func listAdapter(_ listAdapter: ListAdapter, sectionControllerFor object: Any) -> ListSectionController {
2    // 此object的类型就是更新的self.objects的item类型
3    switch object {
4    case is MomentInfo:
5        // MomentBindingSection 继承自 ListBindingSectionController
6        let section = MomentBindingSection()
7        return section
8    default:
9        fatalError()
10    }
11}

使用ListBindingSectionController绑定多个cell

  1. 创建UICollectionViewCell⚠️只能有一种样式布局,不同样式需要不同的cell
1class MomentTopCell: UICollectionViewCell {
2    ...
3}
4extension MomentTopCell: ListBindable {
5    func bindViewModel(_ viewModel: Any) {
6        guard let viewModel = viewModel as? MomentInfo else { return }
7        // 更新cell数据
8    }
9}
  1. 获取cell对象
1func momentTopCell(at index: Int) -> MomentTopCell {
2//  guard let cell = collectionContext?.dequeueReusableCell(withNibName: MomentTopCell.jx_className, bundle: nil, for: self, at: index) as? MomentTopCell else { fatalError() }
3    guard let cell = collectionContext?.dequeueReusableCell(of: MomentTopCell.self, for: self, at: index) as? MomentTopCell else { fatalError() }
4    // 传入cell数据
5    cell.bindViewModel(object!)
6    return cell
7}
  1. 绑定视图模型(ViewModel),可使用枚举替代
1enum ViewModelEnum: String {
2    case top, header, image_single, location, bottom
3}
4/// 绑定viewmodels,用以区分不同cell
5func sectionController(_ sectionController: ListBindingSectionController<ListDiffable>, viewModelsFor object: Any) -> [ListDiffable] {
6    guard let object = object as? MomentInfo else { return [] }
7    var results: [ListDiffable] = []
8    if object.userInfo != nil {
9        results.append(ViewModelEnum.top.rawValue as ListDiffable)
10    }
11    if object.images.count == 1 {
12        results.append(ViewModelEnum.image_single.rawValue as ListDiffable)
13    }else {
14        results.append(ViewModelEnum.header.rawValue as ListDiffable)
15    }
16    if !object.location.isEmpty {
17        results.append(ViewModelEnum.location.rawValue as ListDiffable)
18    }
19    results.append(ViewModelEnum.bottom.rawValue as ListDiffable)
20    return results
21}
22
23/// 不同viewmodel对应的cell
24func sectionController(_ sectionController: ListBindingSectionController<ListDiffable>, cellForViewModel viewModel: Any, at index: Int) -> UICollectionViewCell & ListBindable {
25    let viewModel = ViewModelEnum(rawValue: viewModel as! String)!
26    switch viewModel {
27    case .top:
28        return momentTopCell(at: index)
29    case .image_single:
30        return momentHeaderImageCell(at: index)
31    case .header:
32        return momentHeaderCell(at: index)
33    case .location:
34        return momentLocationCell(at: index)
35    case .bottom:
36        return momentBottomCell(at: index)
37    }
38}
39/// 不同cell对应的size
40func sectionController(_ sectionController: ListBindingSectionController<ListDiffable>, sizeForViewModel viewModel: Any, at index: Int) -> CGSize {
41    guard let object = object as? MomentInfo else { fatalError() }
42    let viewModel = ViewModelEnum(rawValue: viewModel as! String)!
43    let width: CGFloat = collectionContext!.containerSize(for: self).width
44    switch viewModel {
45    case .top:
46        return CGSize(width: width, height: 400)
47    case .header, .image_single:
48        return CGSize(width: width, height: object.cellHeight)
49    case .location:
50        return CGSize(width: width, height: 30)
51    case .bottom:
52        return CGSize(width: width, height: 30)
53    }
54}
  • 标题: IGListKit使用
  • 作者: Jerome Xiong
  • 创建于: 2020-04-29 19:33:57
  • 链接: https://jeromexiong.github.io/2020/04/29/iOS-Swift/IGListKit使用/
  • 版权声明:本文章采用 CC BY-NC-SA 4.0 进行许可。
评论