Iglistkit: Getting index of the currently visible cell in embedded section

Created on 4 May 2017  路  3Comments  路  Source: Instagram/IGListKit

New issue checklist

  • [x] I have reviewed the README and documentation
  • [x] I have searched existing issues and this is not a duplicate (It is but the original issue is closed without an answer)

General information

  • IGListKit version: 2.1.0
  • iOS version(s): 10.2.1
  • CocoaPods/Carthage version: CocoaPods1.2.0
  • Xcode version: 8.3.2
  • Devices/Simulators affected: iPhone 6 Plus
  • Reproducible in the demo project? (Yes/No): Yes
  • Related issues: #464

There is a similar issue (#464) which is closed and doesn't have an answer to my question. Tried posting a reply there but didn't get any feedback in a week - thus creating a new issue.

I'm facing the same challenge as @rcholic: following HorizontalSectionController example I've implemented a vertical list which has got horizontally swipeable sections with additional cells showing some supplementary information. Basically what I'm trying to implement is something similar to Instagram's own photos collection which leads to the change of the little dots mark below indicating currently showing cell.

I intend on implementing this "dotsView" as a separate cell which needs to receive "cell changed" events from the swipe section. The reason for using separate cells & embedded section is that views are fairly complicated and I'd prefer setting up individual cells instead of one massive cell with scroll view

I'm also struggling to get the index of the currently showing cell within embedded section. Embedded section's adapter's delegate methods are only called on display of the section itself but not after the swipes within the section

question

Most helpful comment

@drinkius as long as the cells are within the same section controller, you can wire up the section controller as the collectionViewDelegate on the embedded adapter. Here's some psuedocode:

class MySectionController: IGListSectionController, UICollectionViewDelegate

  // track the displayed cell
  var visibleIndex = 0

  let adapter: IGListAdapter = {
    let adapter = // init
    adapter.dataSource = self
    adapter.collectionViewDelegate = self
    return adapter
  }()

  override func cellForItem(at index: Int) -> UICollectionViewCell {
    let cell = // dequeue the cell
    if let cell = cell as? EmbeddedCell {
      adapter.collectionView = cell.collectionView
    } else if let cell = cell as? DotCell {
      cell.updateSelectedDot(dot: visibleIndex, animated: false)
    }
  }

  func collectionView(_ collectionView: UICollectionView, 
    willDisplay cell: UICollectionViewCell, 
    forItemAt indexPath: IndexPath) {
    visibleIndex = indexPath.section
    if let cell = collectionContext?.cellForItem(index: 1, sectionController: self) as? DotCell {
      cell.updateSelectedDot(dot: visibleIndex, animated: true)
    }
  }

}

All 3 comments

@drinkius as long as the cells are within the same section controller, you can wire up the section controller as the collectionViewDelegate on the embedded adapter. Here's some psuedocode:

class MySectionController: IGListSectionController, UICollectionViewDelegate

  // track the displayed cell
  var visibleIndex = 0

  let adapter: IGListAdapter = {
    let adapter = // init
    adapter.dataSource = self
    adapter.collectionViewDelegate = self
    return adapter
  }()

  override func cellForItem(at index: Int) -> UICollectionViewCell {
    let cell = // dequeue the cell
    if let cell = cell as? EmbeddedCell {
      adapter.collectionView = cell.collectionView
    } else if let cell = cell as? DotCell {
      cell.updateSelectedDot(dot: visibleIndex, animated: false)
    }
  }

  func collectionView(_ collectionView: UICollectionView, 
    willDisplay cell: UICollectionViewCell, 
    forItemAt indexPath: IndexPath) {
    visibleIndex = indexPath.section
    if let cell = collectionContext?.cellForItem(index: 1, sectionController: self) as? DotCell {
      cell.updateSelectedDot(dot: visibleIndex, animated: true)
    }
  }

}

Going to close as resolved. 馃槃

Love you IGLISTKIT <3

Was this page helpful?
0 / 5 - 0 ratings