Max Phillips
Max Phillips

Reputation: 7489

Find Index Number Of A Specific UICollectionView Cell

I have a UICollectionView that returns 77 cells. I'm trying to figure out how to point to a specific cell, for instance the cell at row 2, column 4. To boil it down to my very basic need I need to write a statement that would do this:

if cell[46] == cell[12] {
println("they match")
}

I do not know where to start with this task. I started by trying to print the indexPath that is being used in my prepareForSegue method but that's not returning anything.

This collection view is using dynamic cells. Would it any difference to make them static cells?

Upvotes: 0

Views: 801

Answers (2)

Ian
Ian

Reputation: 12758

There exists collectionView?.visibleCells() which returns [AnyObject] which you can cast to be of type UICollectionViewCell so you can get an array of on screen cells. Then you can access specific cells using subscript notation for comparison, but I still don't think directly comparing the cells is the best approach and if you can store your data in such a way that you can easily access the values without making a request to the server each time, that would be a much better approach for what you're attempting to accomplish.

Upvotes: 1

BHendricks
BHendricks

Reputation: 4493

As mentioned by @jkaufman, you need to use a method that gets you a cell from an index path. func cellForItemAtIndexPath(_ indexPath: NSIndexPath) -> UICollectionViewCell? is the method you would need, and you can pass it an index path, which can be constructed with a section and row index. From there, you could get each of your cells, and do the comparison after retrieving both cells from the cellForItemAtIndexPath.

Function documentation: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionView_class/#//apple_ref/occ/instm/UICollectionView/cellForItemAtIndexPath:

Upvotes: 0

Related Questions