ken*_*ken 0 ios swift iglistkit
我正在使用 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
}
Run Code Online (Sandbox Code Playgroud)
以下是 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) {}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,返回的sizeForItem()宽度比框架宽度足够小。但是,输出列变为一行。
基于这篇文章(在底部)如何创建一个对象,你应该像这样创建你的对象:
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
}
}
Run Code Online (Sandbox Code Playgroud)
你的视图控制器应该是这样的:
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
}}
Run Code Online (Sandbox Code Playgroud)
你的 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) {}}
Run Code Online (Sandbox Code Playgroud)
所以我这里改变的是你添加了5个部分,更好的做法是添加一个部分和5行,这段代码输出你想要的东西,试试吧。
| 归档时间: |
|
| 查看次数: |
1844 次 |
| 最近记录: |