我正在使用 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()
宽度比帧宽度小。但是,输出列变为一行。