Next object instance IGListKit

306 Views Asked by At

Edit: I am new to the field, I did not get a response. Can anyone tell me if I am missing some information? or how I could improve it?

I want an instance from the next object to set a Label text in my current cell cell.Label.text = talents(nextIndex).name //<- Something of this sort

Tried: passing array input to SectionController to use as talents[index+1] Error: File out of range

My Section Controller

class SectionController: ListSectionController {

    var talents: Talent!
    weak var delegate: SectionControllerDelegate?

}

extension SectionController {

    override func sizeForItem(at index: Int) -> CGSize {
        guard
            let context = collectionContext
            else {
                return .zero
        }
        let width = context.containerSize.width
        let height = context.containerSize.height
        return CGSize(width: width, height: height)
    }

    override func cellForItem(at index: Int) -> UICollectionViewCell {    

        guard let cell = collectionContext?.dequeueReusableCellFromStoryboard(withIdentifier: "HomeCell",
                                                                              for: self,
                                                                              at: index) as? HomeCell else {
                                                                                fatalError()
        }

        cell.nameLabel.text = talents.name
        cell.descView.text = talents.largeDesc
        cell.shortDesc.text = talents.smallDesc 

//        let nextTalent = talents[index+1]
//        cell.nextIntroFunc(nextlabels: nextTalent)

        return cell
    }

    override func didUpdate(to object: Any) {
        talents = object as? Talent
    }

}

My ListAdapterDataSource

extension Home: ListAdapterDataSource {

  func objects(for listAdapter: ListAdapter) -> [ListDiffable] {
    print(talents)
    return talents
  }


  func listAdapter(_ listAdapter: ListAdapter, sectionControllerFor object: Any)
  -> ListSectionController {
    return SectionController()
  }

  func emptyView(for listAdapter: ListAdapter) -> UIView? {
    print("emptyView")
    return nil
  }
}

1

There are 1 best solutions below

0
bkbkchoy On

talents in the section controller isn't an array. It's just a single Talent, so calling [index + 1] on it is going to throw an error. (You might want to rename the property of the model in the section controller to talent so it's less confusing.)

Best practice for ListSectionController is to keep a 1:1 relationship between your object model and the section controller. If you want to have reference to the next object, you could create a cell/view model layer based on talent that includes reference to the next talent as well.

For example:

class TalentSectionModel: ListDiffable {
    let talent: Talent
    let nextTalent: Talent?
   
    init(_ talent: Talent, nextTalent: Talent?) {
        self.talent = talent
        self.nextTalent = nextTalent
    }
    
    // whatever properties you want to expose
  
    var name: String {
        return talent.name
    }
   
    // example of exposing info about next talent
    var nextTalentName: String? {
      return nextTalent?.name
    }
  
    // ListDiffable protocol
    // make sure to implement these two so diffing works
    func diffIdentifier() -> NSObjectProtocol {}
    func isEqual(toDiffableObject object: ListDiffable?) -> Bool {}
}

You could also just only pass in the necessary properties from the next talent without passing in the entire nextTalent if you don't want the section model to store the entire object.

Now with the TalentSectionModel you would map over your talents array in Home and return an array of view models in objects() under ListAdapterDataSource. Then, in your section controller you'll have the next talent's information available to you through the view model.

Resources: ListKit Modeling and Binding (talks about the ListBindingSectionController, but also helpful to read for general understanding of model and section controller relationship), ListKit best practices