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
}
}
talentsin the section controller isn't an array. It's just a singleTalent, 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 totalentso 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
talentthat includes reference to the next talent as well.For example:
You could also just only pass in the necessary properties from the next talent without passing in the entire
nextTalentif you don't want the section model to store the entire object.Now with the
TalentSectionModelyou would map over your talents array inHomeand return an array of view models inobjects()underListAdapterDataSource. 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