IGListKit - 添加多个部分并为每个部分添加一个补充 Header 视图

IGListKit - Add Multiple Sections with a Supplementary Header View for each

我正在尝试在 UICollectionView 中实现股票订单和拥有股票的列表。我正在使用 IGListKit 来实现 collection 视图,我能够让单元格正确显示。当我尝试将 Supplementary View 添加到 OrderStockFeedListSectionControllerOwnedStockFeedListSectionController 以创建header 查看。


如果我只是传递订单股票的模型,我会得到我期望的 header:


但是当我通过两个模型(订购股票和拥有股票)时,我得到了一堆重复的 headers,甚至出于某种奇怪的原因甚至是页脚。

TSLA 和 WF 是订单库存 | AAPL 和 MSFT 是自有股票


我已经看过 IGListKit 的文档并像他们那样实现了 header 视图,但我似乎无法添加多个 headers.

ListAdapter 数据源


    func objects(for listAdapter: ListAdapter) -> [ListDiffable] {
        var orderList: [OrderStockDataBucket] = []
        orderList.append(OrderStockDataBucket(Symbol: "TSLA", Name: "Tesla Motors", Price: 231.32, PercentChange: 2.32))
        orderList.append(OrderStockDataBucket(Symbol: "WF", Name: "Wells Fargo", Price: 231.32, PercentChange: 2.32))
        let orderStockDataBatch = OrderStockDataBatch(List: orderList)

        var ownedList: [OwnedStockDataBucket] = []
        ownedList.append(OwnedStockDataBucket(Symbol: "AAPL", Name: "Apple Inc.", Price: 123.21, PercentChange: 2.32))
        ownedList.append(OwnedStockDataBucket(Symbol: "MSFT", Name: "Microsoft Corp.", Price: 231.23, PercentChange: 22.32))
        let ownedStockDataBatch = OwnedStockDataBatch(List: ownedList)

        return [orderStockDataBatch, ownedStockDataBatch] as [ListDiffable]
    }

    func listAdapter(_ listAdapter: ListAdapter, sectionControllerFor object: Any) -> ListSectionController {
        if let _ = object as? OrderStockDataBatch {
            return OrderStockFeedListSectionController()
        }
        else if let _ = object as? OwnedStockDataBatch{
            return OwnedStockFeedListSectionController()
        }
        print("List Adapter object did not match any type!")
        return ListSectionController()
    }

    func emptyView(for listAdapter: ListAdapter) -> UIView? {
        nil
    }



OrderStockFeedListSectionControllerOwnedStockFeedListSectionController完全一样,我只是将 header 视图标签更改为 "Your Stocks")

import UIKit
import IGListKit

class OrderStockFeedListSectionController: ListSectionController, ListSupplementaryViewSource {

    private var orderStockDataBatch: OrderStockDataBatch?

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

    override func numberOfItems() -> Int {
        return orderStockDataBatch?.orderStockDataList.count ?? 0
    }

    override func sizeForItem(at index: Int) -> CGSize {
        let containerWidth = self.collectionContext?.containerSize.width ?? 50
        let containerHeight = self.collectionContext?.containerSize.height ?? 50
        let width: CGFloat!
        if (containerWidth < containerHeight) {
            width = containerWidth
        } else {
            width = containerHeight
        }
        let height = CGFloat(OwnedStockFeedCollectionViewCell.CELL_HEIGHT)
        return CGSize(width: width, height: height)
    }

    override func cellForItem(at index: Int) -> UICollectionViewCell {
        let cell = collectionContext!.dequeueReusableCell(of: OwnedStockFeedCollectionViewCell.self, for: self, at: index) as! OwnedStockFeedCollectionViewCell

        cell.symbolLabel.text = orderStockDataBatch?.orderStockDataList[index].stockSymbol
        cell.nameLabel.text = orderStockDataBatch?.orderStockDataList[index].stockName
        cell.priceLabel.text = "\(orderStockDataBatch?.orderStockDataList[index].stockPrice ?? 0)"
        cell.percentChangeLabel.text = "\(orderStockDataBatch?.orderStockDataList[index].stockPercentChange ?? 0)"
        //cell.percentChangeLabel.backgroundColor = UIColor(named: "MarketGreen")

        return cell
    }

    override func didUpdate(to object: Any) {
        orderStockDataBatch = object as? OrderStockDataBatch
    }



    func supportedElementKinds() -> [String] {
        return [UICollectionView.elementKindSectionHeader]
    }

    func viewForSupplementaryElement(ofKind elementKind: String, at index: Int) -> UICollectionReusableView {
        return userHeaderView(atIndex: index)
    }

    func sizeForSupplementaryView(ofKind elementKind: String, at index: Int) -> CGSize {
        return CGSize(width: collectionContext!.containerSize.width, height: 25)
    }

    private func userHeaderView(atIndex index: Int) -> UICollectionReusableView {
        let view = collectionContext!.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, for: self, class: UICollectionReusableView.self, at: index)

        let label = UIInsetLabel(frame: view.frame)
        label.text = "Your Orders"
        label.textColor = UIColor(named: "DarkLightGray")
        label.font = UIFont(name: "HelveticaNeue-Bold", size: 16)
        label.backgroundColor = UIColor(named: "LightDarkGray")
        label.contentInsets = UIEdgeInsets(top: 0, left: 15, bottom: 0, right: 0)
        view.addSubview(label)

        return view
    }

}



几天来我一直在寻找答案,但我一无所获。请帮忙。

我的问题又是ugur pointed out. I just needed to extend a UICollectionReusableView on my own view. Then I would just obtain that view type when dequeueing the view. Thanks ugur