Nonouf
Nonouf

Reputation: 388

IGListKit with sections and multiple items

I'm trying to implement a collection view with IGListKit. It can either have one or three sections. The datasource is populated in realtime and so needs to refresh the content when new data are available.

To do so, I've created a datasource object conform to IGListDiffable that represent each section as such:

final class DataSource: NSObject {
  var title: String?
  var items: [SJResult] = []
}

extension DataSource: IGListDiffable {
  public func diffIdentifier() -> NSObjectProtocol {
    return self
  }

  public func isEqual(toDiffableObject object: IGListDiffable?) -> Bool {
    if object === self {
      return true
    }
    guard let obj = object as? DataSource else {
      return false
    }
    return obj.items.count == items.count
  }
}

Inside each section, I would like to display a list of items. I've managed to create the section by reusing the same section controller, but the only way I've found to insert new result was to do call reloadData() on the adapter. This is pretty bad as it will reload the complete list of items (which can potentially be very long, >50 items). It also give no possibility to animate the insertion of new item. I've also tried to do a adapter.performUpdates(animated: true) with no luck as it only refresh my datasource but not the items inside the datasource.

I then tried to create a section controller which will display each section with one cell. The cell would contain a IGListCollectionView and will take care of display the items, but with this I had no luck as well as despite the second section controller receives the objects it never displays the cells.

I'm a bit struggling now and not sure how I could display three different section that use the same type of cells with IGListKit, by having the sections static and the items dynamic. I'm thinking of creating three IGListCollectionView and setup bottom / top constraints between them, but I'm having some doubt on the possibility of doing that.

I'm wondering if someone already came across a similar issue and/or if the guys from IGListKit could give me some hints of what's the best implementation to resolve that?

Upvotes: 0

Views: 3540

Answers (1)

rnystrom
rnystrom

Reputation: 1916

You should be returning unique instances of your section controllers for each object. Don't reuse them!

Another thing to note is that you're using self as the diff identifier which means that the instance of the object identifies its uniqueness. That means two DataSource objects wont ever be compared (obj.items.count == items.count will never happen). Not a deal-breaker, but just be aware that's how it'll behave.

You also might want to take a look at IGListBindingSectionController which takes your original model and breaks it down into view models that drive each cell in the section.

More details and an example in the pull request. Note that this requires using master if you're using CocoaPods.

https://github.com/Instagram/IGListKit/pull/494

Upvotes: 1

Related Questions