Instagram / IGListKit

A data-driven UICollectionView framework for building fast and flexible lists.
https://instagram.github.io/IGListKit/
MIT License
12.87k stars 1.54k forks source link

Get cell indexPath and data via CGPoint location #439

Closed shuhrat10 closed 7 years ago

shuhrat10 commented 7 years ago

Hi,

I have IGListKit with multiple sections which contain embedded IGCollectionView. I wonder how can I get master and child IndexPath from UIViewController and then get data for each IndexPath.

Thanks!

rnystrom commented 7 years ago

@shuhrat10 sure thing, should be really easy! And you don't have to use any IGListKit stuff in fact:

let path = collectionView.indexPathForItem(at: point)
let object = adapter.object(atSection: path.section)

I wonder if this would be a good API on IGListAdapter @jessesquires?

shuhrat10 commented 7 years ago

@rnystrom I did that, but I'm getting only section. I think once I get section cell, I need to convertPoint to get IndexPath from child IGCollectionView.

rnystrom commented 7 years ago

Ooooh, embedded collection view. I skipped over that. Here's a quick thought that might get you on the right path:

let path = collectionView.indexPathForItem(at: point)
let object = adapter.object(atSection: path.section)
if let cell = collectionView.cellForItem(at: path) as? EmbeddedCollectionViewCell,
    let sectionController = adapter.sectionController(for: object) as? HorizontalSectionController {
    let subPoint = cell.collectionView.convert(point, from: collectionView)
    let subPath = cell.collectionView.indexPathForItem(at: subPoint)
    let subObject = sectionController.adapter.object(atSection: subPath.section)
}

I'm using classes in our example project, assuming your setup is similar.

shuhrat10 commented 7 years ago

@rnystrom thank you very much! It's working as expected