美文网首页iglistkit
IGListKit的使用

IGListKit的使用

作者: Grabin | 来源:发表于2018-04-28 13:01 被阅读930次

IGListKit 原文文档:🔗原文Link
IGListKit 中文文档:🔗中文文档Link

image.png IG.gif

IGListKit能实现这样的需求,不去reload整个界面,还有动画什么的.....

使用过程:

  1. 在UIViewController上创建collectionView集成IGListCollectionView
  2. 实例化一个IGListAdapter用于Data处理
let collectionView = IGListCollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())

lazy var adapter: IGListAdapter = {
        return IGListAdapter(updater: IGListAdapterUpdater(), viewController: self, workingRangeSize: 0)
    }()
  1. 将创建的collectionView 赋值给 实例化的IGListAdapter对象(adapter) 的collectionView,
    并且遵守IGListAdapter dataSource协议,实现代理方法
class FeedViewController: UIViewController {
    
    let collectionView = IGListCollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
    
    lazy var adapter: IGListAdapter = {
        return IGListAdapter(updater: IGListAdapterUpdater(), viewController: self, workingRangeSize: 0)
    }()
    
    let loader = JournalEntryLoader()
    
    let pathfinder = Pathfinder()
    
    let wxScanner = WxScanner()
    
    override func loadView() {
        view = collectionView
        view.backgroundColor = .black
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        loader.loadLatest()
        adapter.collectionView = collectionView
        adapter.dataSource = self
    }

}

实现代理方法


image.png
extension FeedViewController: IGListAdapterDataSource {
    func objects(for listAdapter: IGListAdapter) -> [IGListDiffable] {
        var items: [IGListDiffable] = [wxScanner.currentWeather]
        items += pathfinder.messages as [IGListDiffable]
        items += loader.entries as [IGListDiffable]
        return items.sorted { (left: Any, right: Any) -> Bool in
            if let left = left as? DateSortable, let right = right as? DateSortable {
                return left.date > right.date
            }
            return false
        }
    }
    
    func listAdapter(_ listAdapter: IGListAdapter, sectionControllerFor object: Any) -> IGListSectionController {
        if object is Message {
            return MessageSectionController()
        } else if object is Weather {
            return WeatherSectionController()
        }
        return JournalSectionController()
    }
    
    func emptyView(for listAdapter: IGListAdapter) -> UIView? {
        return nil
    }
}
  1. 创建IGListSectionController,这里以demo中的WeatherSectionController为例

import UIKit
import IGListKit

class WeatherSectionController: IGListSectionController {
    
    var weather: Weather!
    
    var expanded = false
    
    override init() {
        super.init()
        inset = UIEdgeInsetsMake(0, 0, 15.0, 0)
    }

}

extension WeatherSectionController: IGListSectionType {
    func numberOfItems() -> Int {
        return expanded ? 5 : 1
    }
    
    func sizeForItem(at index: Int) -> CGSize {
        guard let context = collectionContext else { return .zero }
        let width = context.containerSize.width
        if index == 0 {
            return CGSize(width: width, height: 70.0)
        }
        return CGSize(width: width, height: 40.0)
    }
    
    func cellForItem(at index: Int) -> UICollectionViewCell {
        let cellClass = index == 0 ? WeatherSummaryCell.self : WeatherDetailCell.self
        let cell = collectionContext!.dequeueReusableCell(of: cellClass, for: self, at: index)
        if let cell = cell as? WeatherSummaryCell {
            cell.setExpanded(expanded)
        } else if let cell = cell as? WeatherDetailCell {
            let title: String, detail: String
            switch index {
            case 1:
                title = "SUMRISE"
                detail = weather.sunrise
            case 2:
                title = "SUMSET"
                detail = weather.sunset
            case 3:
                title = "HIGH"
                detail = "\(weather.high) C"
            case 4:
                title = "LOW"
                detail = "\(weather.low) C"
            default:
                title = "n/a"
                detail = "n/a"
            }
            cell.titleLabel.text = title
            cell.detailLabel.text = detail
        }
        return cell
    }
    
    func didUpdate(to object: Any) {
        weather = object as? Weather
    }
    
    func didSelectItem(at index: Int) {
        expanded = !expanded
        collectionContext?.reload(self)
    }
    
    
}
创建sectionController要注意的点是:

a. 创建的sectionController要继承IGListSectionController
b. 并且要实现 IGListSectionType 的代理方法才能被IGListKit使用
c. 数据源在 func didUpdate(to object: Any) 代理里面实现
d. 在sectionController里面使用 collectionContext?.reload(self) 可以刷新数据

相关文章

网友评论

    本文标题:IGListKit的使用

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