Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom cell to the end of collectionView #70

Closed
lingzlu opened this issue Oct 20, 2016 · 3 comments
Closed

Add custom cell to the end of collectionView #70

lingzlu opened this issue Oct 20, 2016 · 3 comments

Comments

@lingzlu
Copy link

lingzlu commented Oct 20, 2016

How can I add an additional cell to the end of the collectionView? with native implementation, I would do something as follow:

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return items.count + 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    if indexPath.row < items.count {
        let cell =  collectionView.dequeueReusableCell(withReuseIdentifier: "RegularCell", for: indexPath) as! RegularCell
        // configure regular cell 
        return cell
    }

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SpecialCell", for: indexPath) as! SpecialCell
    // this cell has different layout with no SectionModel item, configureCell does not get called
    return cell
}

Can I achieve something similar with RxDataSource?

@gkuhlmann14
Copy link

gkuhlmann14 commented Oct 20, 2016

Hi @lingzlu have you taken a look at using a datasource? Something like:
let dataSource = RxCollectionViewSectionedReloadDataSource<YourCustomSectionModel>
There's a pretty good example in the examples on it.
You could then do the same kind of thing from the examples:

 func skinTableViewDataSource(dataSource: RxCollectionViewSectionedReloadDataSource<YourCustomSectionModel>) { dataSource.configureCell = { (dataSource, table, idxPath, item) in 
    switch dataSource.itemAtIndexPath(idxPath) { 
      case RegularCell:
        let cell =  collectionView.dequeueReusableCell(withReuseIdentifier: "RegularCell", for: indexPath) as! RegularCell
        // configure regular cell 
        return cell
       case SepcialCell:
         let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SpecialCell", for: indexPath) as! SpecialCell
    // this cell has different layout with no SectionModel item, configureCell does not get called
      return cell

Then in your viewDidLoad you'll want to have some sort of observable of (regular) [SectionItem] that maps to [YourCustomSectionModel] which you would bind to collectionView.rx_itemsWithDataSource(dataSource)

.map{ regularSectionItems -> [YourCustomSectionModel] in 
  let specialCell = SectionItem.SpecialSectionItem()
  let specialSection = YourCustomSectionModel.SpecialSection(title: "Special", items: [specialCell]) 
  let regularSection = YourCustomSectionModel.RegularSection(title: "Regular", items: regularSectionItems) 
  return [regularSection, specialSection]} 
.bindTo(collectionView.rx_itemsWithDataSource(dataSource)) 
.addDisposableTo(disposeBag) 

Give that a shot

@lingzlu
Copy link
Author

lingzlu commented Oct 20, 2016

thank gkuhlmann14! I model the example on MultipleSectionModelViewController.swift, and got something working, only problem is that I had to give SectionModel for special cell a dummy item to trigger the configureCell.

@kzaher
Copy link
Member

kzaher commented Oct 21, 2016

This looks resolved.

@kzaher kzaher closed this as completed Oct 21, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants