如何在swift中使用IGListKit创建两列集合视图列表

时间:2018-01-04 12:40:21

标签: ios swift iglistkit

我正在使用IGListKit库(IGListKit)。 我想创建一个两列集合视图列表,如下所示。 我已经阅读了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

3 个答案:

答案 0 :(得分:3)

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

并且您的viewcontroller应如下所示:

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

答案 1 :(得分: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很好地填充单元格。

答案 2 :(得分:0)

单个单元格/单个部分

对于那些希望每节使用单个单元格的人,不同于在单个sectionSectionController中使用多个单元格/行的已接受答案,您只需使用IGListKit的专用布局,如下所示:-

client

通过在创建新行之前确保各部分填充宽度,此行为与普通collectionViewLayout相同。