Nikkko
Nikkko

Reputation: 47

IGListKit - How to add cell when there is a value in view model and not add one when there is not?

How to add cell when there is a value in view model and not add one, when there is not? What i mean is that I'm using ListBindingSectionController and for every cell type(image, caption, etc) I have separate ViewModel class. Posts will have pictures, but may not have caption. My question is: How can I add caption cell when there is a caption whit the post, and not add one when there isn't caption?

My Caption ViewModel class:

import Foundation
import IGListKit

final class CaptionViewModel: ListDiffable {

  let fullName: String
  let caption: String? // can be nil(empty). When it is empty, I don't want caption cell inserted.


  init(fullName: String, caption: String?) {
    self.fullName = fullName
    if caption != nil {
      self.caption = caption
    } else {
      self.caption = " "
    }
  }

  //MARK: ListDiffable
  func diffIdentifier() -> NSObjectProtocol {
    return "caption" as NSObjectProtocol
  }

  func isEqual(toDiffableObject object: ListDiffable?) -> Bool {
    // not completed
    return false
  }
}

Upvotes: 0

Views: 347

Answers (1)

Benny Wong
Benny Wong

Reputation: 6851

This determination should happen in the objectsForListAdapter function. In objectsForListAdapter, it should look something like this:

func objectsForListAdapter(listAdapter: ListAdapter) -> [ListDiffable] {
  var objects = []
  // ...snip...
  if captionViewModel.caption != nil {
      object.append(captionViewModel)
  }
  // ...snip...
  return objects
}

Upvotes: 0

Related Questions