未调用cellForItem或sizeForItem

时间:2017-08-31 15:11:57

标签: iglistkit

我是第一次尝试IGListKit,但我似乎早早就碰到了一堵砖墙

lazy var collectionView: UICollectionView = {
    let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
    collectionView.backgroundColor = .white
    return collectionView
}()

override func viewDidLoad() {
    super.viewDidLoad()

    self.view.addSubview(self.collectionView)

    let updater = ListAdapterUpdater()
    let adapter = ListAdapter(updater: updater, viewController: self, workingRangeSize: 0)
    adapter.collectionView = self.collectionView

    adapter.dataSource = self
}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    collectionView.frame = view.bounds
}

extension SocialViewController: ListAdapterDataSource {

func objects(for listAdapter: ListAdapter) -> [ListDiffable] {
    // this can be anything!
    return [ "Foo" as ListDiffable, "Bar" as ListDiffable]
}

func listAdapter(_ listAdapter: ListAdapter, sectionControllerFor object: Any) -> ListSectionController {
    return TopNewsSectionController()
}

func emptyView(for listAdapter: ListAdapter) -> UIView? {
    return UIImageView(image: UIImage(named: "swords"))
}

}

class TopNewsSectionController:ListSectionController {

override func sizeForItem(at index: Int) -> CGSize {
    return CGSize(width: collectionContext!.containerSize.width, height: 55)
}

override func cellForItem(at index: Int) -> UICollectionViewCell {
    return collectionContext!.dequeueReusableCell(of: TopNewsCollectionViewCell.self, for: self, at: index)
}

}

但我的cellForItem或sizeForItem都没有被调用 我做错了什么?

3 个答案:

答案 0 :(得分:0)

调用adapter.performUpdatesAnimatedadapter.reloadDataWithCompletion(首次加载)进行更新

答案 1 :(得分:0)

尝试将适配器声明为类属性而不是方法属性,

    let updater = ListAdapterUpdater()
    let adapter = ListAdapter(updater: updater, viewController: self, workingRangeSize: 0)
    adapter.collectionView = self.collectionView

    lazy var adapter: ListAdapter = {
        return ListAdapter(updater: ListAdapterUpdater(), viewController: self, workingRangeSize: 0)
    }()

答案 2 :(得分:0)

adapter 返回时,您的 viewDidLoad 对象被销毁。尝试将其声明为您的 ViewController 的属性。