gpt4 book ai didi

ios - 如何在 swift 中使用 IGListKit 创建一个两列的 Collection View 列表

转载 作者:行者123 更新时间:2023-11-28 10:11:12 29 4
gpt4 key购买 nike

我正在使用 IGListKit 库(IGListKit)。我想创建一个两列的 Collection View 列表,如下所示。我已经阅读了 IGListKit 手册,但我无法理解如何实现这一目标。

我在 sizeForItem 中传递了适当的宽度,但该列变为 1。

你能给我什么建议吗?

enter image description here

附加信息

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

第一个代码是 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() 返回的宽度比框架宽度足够小。但是,输出列变为一行。

enter image description here

最佳答案

基于 this文章(在底部)如何创建一个对象你应该像这样创建你的对象:

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
}
}

你的 View Controller 应该是这样的:

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 行,这段代码输出你想要的东西,试试吧。

关于ios - 如何在 swift 中使用 IGListKit 创建一个两列的 Collection View 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48095549/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com