IGListKit探索 - 1 基本使用

之前一直觉得Instagram的iOS APP首页的流畅度非常不错。现在他们开源了这个IGListKit框架,总要好好研究研究。

IGListKitDemo.png

核心架构比较清晰。每一个条目就是一种sectionController(如图黄色框内)。每个section对应一个Item。具体结构如下

81DB540E-36D4-4D3F-8651-A8EDC7A6BDC9.png

框架的文档很详细但是是英文版的。

# Latest release of IGListKit
pod 'IGListKit', '~> 2.0.0'

# Use the master branch (we use this at Instagram)
pod 'IGListKit', :git => 'https://github.com/Instagram/IGListKit.git', :branch => 'master'

使用很简单。新建ViewController和someSectionController 继承
IGListSectionController并遵守IGListSectionType。

viewController中设置UI

let layout = UICollectionViewFlowLayout()
let collectionView = IGListCollectionView(frame: .zero, collectionViewLayout: layout)

let updater = IGListAdapterUpdater()
let adapter = IGListAdapter(updater: updater, viewController: self, workingRangeSize: 0)
//set方法调用数据源更新操作 把objects(for listAdapter: IGListAdapter) -> [IGListDiffable] 方法设置的数据传给cectionController
adapter.collectionView = collectionView

接下来设置数据,根据预先分好的组。

func objects(for listAdapter: IGListAdapter) -> [IGListDiffable] {
  // this can be anything!
  return [ "Foo", "Bar", 42, "Biz" ]
}

func listAdapter(_ listAdapter: IGListAdapter, sectionControllerFor object: Any) -> IGListSectionController {
  //根据需求返回不同的sectionController
  return someSectionController()
}
//没有数据时,显示的自定义view
func emptyView(for listAdapter: IGListAdapter) -> UIView? {
  return nil
}

- (void)updateBackgroundViewWithItemCount:(NSUInteger)itemCount {
    UIView *backgroundView = [self.dataSource emptyViewForListAdapter:self];
    // don't do anything if the client is using the same view
    if (backgroundView != _collectionView.backgroundView) {
        // collection view will just stack the background views underneath each other if we do not remove the previous
        // one first. also fine if it is nil
        [_collectionView.backgroundView removeFromSuperview];
        _collectionView.backgroundView = backgroundView;
    }
    _collectionView.backgroundView.hidden = itemCount > 0;
}

//someSectionController中设置数据
  func numberOfItems() -> Int {
        return 4
    }
//section中每一行高
    func sizeForItem(at index: Int) -> CGSize {
        return CGSize(width: collectionContext!.containerSize.width, height: 105)
    }

//collectionContext属性 获取到objects(for listAdapter: IGListAdapter) -> [IGListDiffable]  中设置的data
    func cellForItem(at index: Int) -> UICollectionViewCell {
        let cell = collectionContext!.dequeueReusableCell(of: LabelCell.self, for: self, at: index) as! LabelCell
        let section = collectionContext!.section(for: self)
        cell.label.text = "Section \(section), cell \(index)"
        return cell
    }

    func didUpdate(to object: Any) {}

    func didSelectItem(at index: Int) {}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,816评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,262评论 4 61
  • 我们是初二下半学期在一起的,没错,我是早恋,这也是初恋 。和很多人一样,回忆起初恋时嘴角会不自觉的上扬,我的初恋满...
    喵小姐123阅读 451评论 1 1
  • 没有坚持每天写作,哪怕是随笔,很是愧疚,不为旁人,仅为自己的言辞乏味…… 很多年了,总是计划努力,计划旅行,...
    香蕉啊你个不呐呐阅读 308评论 0 0
  • 无法拒绝别人,却很大程度伤害自己。到头来,苦了自己,却不一定讨好了别人。多么痛的领悟,歌词体现的更彻底,虽听了好多...
    小女来浪浪阅读 267评论 0 0