0

我正在使用 IGListKit 库(IGListKit)。我想创建一个两列集合视图列表,如下所示。我已经阅读了 IGListKit 手册,但我不明白如何实现这一点。

我在 中传递了正确的宽度sizeForItem,但列变为 1。

你能给我什么建议吗?

在此处输入图像描述

附加信息

以下是我的代码及其输出屏幕截图。

第一个代码是 ViewController。

class ViewController: UIViewController {
    lazy var adapter: ListAdapter = {
        return ListAdapter(updater: ListAdapterUpdater(), viewController: self)
    }()

    @IBOutlet weak var collectionView: UICollectionView!

    let channels = [
        Channel(id: "1", color: UIColor.black),
        Channel(id: "2", color: UIColor.blue),
        Channel(id: "3", color: UIColor.red),
        Channel(id: "4", color: UIColor.yellow),
        Channel(id: "5", color: UIColor.green),
        ]

    override func viewDidLoad() {
        super.viewDidLoad()

        self.adapter.collectionView = self.collectionView
        self.adapter.dataSource = self
    }

    func objects(for listAdapter: ListAdapter) -> [ListDiffable] {
        return self.channels as! [ListDiffable]
    }

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

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

以下是SectionController。

final class ChannelSectionController: ListSectionController {

    var channel: Channel?

    override func numberOfItems() -> Int {
        return 1
    }

    override func sizeForItem(at index: Int) -> CGSize {
        let length = collectionContext!.containerSize.width / 4
        return CGSize(width: length, height: length)
    }

    override func cellForItem(at index: Int) -> UICollectionViewCell {
        guard let channel = channel else {
            fatalError("channel is nil.")
        }

        guard let cell = self.collectionContext?.dequeueReusableCellFromStoryboard(withIdentifier: "ChannelCell", for: self, at: index) as? ChannelCollectionViewCell else {
            fatalError()
        }
        cell.channel = channel // Update the cell label and color.
        return cell
    }

    override func didUpdate(to object: Any) {
        self.channel = object as? Channel
    }

    override func didSelectItem(at index: Int) {}
}

如您所见,返回的sizeForItem()宽度比帧宽度小。但是,输出列变为一行。

在此处输入图像描述

4

3 回答 3

5

根据这篇文章(在底部)如何创建对象,您应该像这样创建对象:

class ChannelCollection: ListDiffable {

var id : String
var channels: [Channel]
init(id: String,channels: [Channel]) {
    self.id = id
    self.channels = channels
}
func isEqual(toDiffableObject object: ListDiffable?) -> Bool {
    return true //compare your object here, I returned true for test
}
func diffIdentifier() -> NSObjectProtocol {
    return id as NSObjectProtocol
}
}

你的视图控制器应该是这样的:

class ViewController: UIViewController,ListAdapterDataSource {
lazy var adapter: ListAdapter = {
    return ListAdapter(updater: ListAdapterUpdater(), viewController: self)
}()
var items : [Any] = []
@IBOutlet weak var collectionView: UICollectionView!
var channelCollection : ChannelCollection?
let channels = [
    Channel(id: "1", color: UIColor.black),
    Channel(id: "2", color: UIColor.blue),
    Channel(id: "3", color: UIColor.red),
    Channel(id: "4", color: UIColor.yellow),
    Channel(id: "5", color: UIColor.green),
    ]

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

override func viewDidLoad() {
    super.viewDidLoad()
    self.channelCollection = ChannelCollection.init(id: "1", channels: self.channels)
    self.items.append(self.channelCollection as Any)
    self.adapter.collectionView = self.collectionView
    self.adapter.dataSource = self
}

func objects(for listAdapter: ListAdapter) -> [ListDiffable] {
    return self.items as! [ListDiffable]
}

func listAdapter(_ listAdapter: ListAdapter, sectionControllerFor object: Any) -> ListSectionController {
    return ChannelSectionController.init(channelCollection: self.channelCollection!)
}

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

你的 ListSectionController:

final class ChannelSectionController: ListSectionController {

var channel: Channel?

var channelCollection : ChannelCollection?
init(channelCollection: ChannelCollection)
{
    self.channelCollection = channelCollection
}
override func numberOfItems() -> Int {
    return (self.channelCollection?.channels.count)!
}

override func sizeForItem(at index: Int) -> CGSize {
    let length = collectionContext!.containerSize.width / 4
    return CGSize(width: length, height: length)
}

override func cellForItem(at index: Int) -> UICollectionViewCell {


    guard let cell = self.collectionContext?.dequeueReusableCellFromStoryboard(withIdentifier: "ChannelCell", for: self, at: index) as? ChannelCollectionViewCell else {
        fatalError()
    }
    //cell.channel = channel // Update the cell label and color.
    return cell
}

override func didUpdate(to object: Any) {
    self.channel = object as? Channel
}

override func didSelectItem(at index: Int) {}}

所以我在这里改变的是你添加了5个部分,更好的做法是添加一个部分和5行,这段代码输出你想要的东西,试试吧。

于 2018-01-04T13:33:09.713 回答
2

单细胞/单节

对于那些想要在每个部分使用单个单元格的人,这与在单个 sectionSectionController 中使用多个单元格/行的公认答案不同,您只需使用 IGListKit 的专用布局,如下所示:-

let layout = ListCollectionViewLayout(stickyHeaders: false, topContentInset: 0, stretchToEdge: true)

这将与普通 collectionViewLayout 的行为方式相同,方法是确保各部分在创建新行之前填充宽度。

于 2019-09-07T23:08:41.887 回答
0

只需在部分控制器中设置每个项目的大小。

sizeForItem(at index: Int)通过在您的部分控制器中覆盖来执行此操作。

例如:假设每个单元格之间有 16 像素的空间,并且想要使用 2 列布局:

override func sizeForItem(at index: Int) -> CGSize {
  guard let contextOfCollection = collectionContext, 
    let entry = entry 
    else { 
      return .zero
  }

  let objectWidth = (contextOfCollection.containerSize.width - 16 - 16)/2 // dividing by 2 because
                                                                          // you want 2 columns
  return CGSize(width: objectWidth, height:objectWidth)
}

或者你可以设置一个固定的单元格大小并使用 insets/autolayout/constraints 很好地填充单元格。

于 2019-08-29T09:09:58.343 回答