-1

我正在尝试在 UICollectionView 中实现股票订单和拥有股票的列表。我正在使用IGListKit来实现集合视图,并且能够正确显示单元格。当我尝试将补充视图添加到OrderStockFeedListSectionControllerOwnedStockFeedListSectionController以创建标题视图时,就会出现问题。


如果我只是传递 Order Stocks 的模型,我会得到我期望的标题:

在此处输入图像描述


但是,当我通过两种模型(订单股票和自有股票)时,我会得到大量重复的页眉,甚至出于某种奇怪的原因还有页脚。

在此处输入图像描述

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


我已经查看了IGListKit的文档并像他们一样实现了标题视图,但我似乎无法添加多个标题。

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
    }



OrderStockFeedListSectionController (OwnedStockFeedListSectionController完全相同,我只是将标题视图标签更改为“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
    }

}



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

4

1 回答 1

2

我的问题是ugur指出的。我只需要在我自己的视图上扩展一个 UICollectionReusableView 。然后我会在出列视图时获取该视图类型。再次感谢ugur 。

于 2020-04-29T22:47:12.260 回答