Iglistkit: Cannot get the index of the currently visible cell(s)

Created on 5 Feb 2017  路  7Comments  路  Source: Instagram/IGListKit

New issue checklist

General information

  • IGListKit version: 2.0.0
  • iOS version(s): 10.0
  • CocoaPods/Carthage version:
  • Xcode version: 8.2.1
  • Devices/Simulators affected:
  • Reproducible in the demo project? (Yes/No):
  • Related issues:

I am trying to use IGListKit to render custom collection view and table view, where I have a horizontal scrollview in a section controller. In this horizontal scrollview, I need to get the index of the currently visible cells.

I tried to use the displayDelegate in the section controller, but it seems to me that the following delegate method is triggered only once when the section controller is initially loaded:

    func listAdapter(_ listAdapter: IGListAdapter, willDisplay sectionController: IGListSectionController, cell: UICollectionViewCell, at index: Int)

So I was wondering if there's a way in IGListKit to get the index of the currently visible cells. Thanks!

question

Most helpful comment

@rcholic it's a little tedious, but you can do something like this for now:

let visibleCells = collectionContext!.visibleCells(sectionController: self)
var indexes = [Int]()
for cell in visibleCells {
  // technically this method can return NSNotFound, but if the cell is visible it should have an index
  indexes.append(collectionContext!.index(cell:cell, sectionController: self))
}

I def think this would be a nice API to have though.

All 7 comments

@rcholic can you give a little more detail on the horizontal scrolling list? Is that an embedded IGListAdapter?

Are you asking if there's a way to do something like collectionContext?.visibleIndexes from within a section controller, similar to the visible cells API?

@rnystrom thanks for your prompt reply.

To answer the second part of your reply/question, yes, I am looking for a method in IGListKit that can return the indices of visible cells in a section controller, however, I did not know the method visibleCells available on collectionContext.

The horizontal scrolling list comes from the HorizontalSectionController code in the IGListExamples folder in this repository. Because I need to keep track of the visible cells/indices when this horizontal section controller scrolls, I implemented IGListDisplayDelegate on it. But the problem I am running into is the delegate methods listAdapter(_ listAdapter: _ willDisplay: _ cell: _ at:) runs only once, which is upon the initial loading of the section controller.

So is there a way to get the index of the visible cells when the section controller scrolls? I appreciate your help!

@rcholic it's a little tedious, but you can do something like this for now:

let visibleCells = collectionContext!.visibleCells(sectionController: self)
var indexes = [Int]()
for cell in visibleCells {
  // technically this method can return NSNotFound, but if the cell is visible it should have an index
  indexes.append(collectionContext!.index(cell:cell, sectionController: self))
}

I def think this would be a nice API to have though.

@rcholic If this unblocks you mind if we close the issue?

Thanks again @rnystrom

I tried the code you wrote above, and I found that it works well on section controller that has a portrait orientation (list scrolls top/bottom), however, it cannot return the correct indices of visible cells if they are embedded in a horizontal section controller (scrolls left/right) - all the returned index is 0.

In addition, I tried the IGListDisplayDelegate way of getting the indices of visible cells. Same finding: it works on section controller scrolling top/bottom, but keeps returning 0 index on section controllers scrolling left/right.

@rcholic oh! Now I understand a little better. So you are trying to collect the indices of the visible sections. Give these methods a shot using the adapter.

@rnystrom 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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

racer1988 picture racer1988  路  3Comments

rnystrom picture rnystrom  路  3Comments

shuhrat10 picture shuhrat10  路  3Comments

krysztalzg picture krysztalzg  路  3Comments

omerfarukyilmaz picture omerfarukyilmaz  路  3Comments