How to get indexpath from row index Swift

Saira Nawaz picture Saira Nawaz · Jan 17, 2017 · Viewed 27k times · Source

I am loading an array to UIcollectionview(later will add other data) . I want to select collection view item randomly as:

var indexpath = Int(arc4random_uniform(UInt32(items.count)-1) 

self.collectionview.selectitem(at: **indexpath**, animated:true ,scrollPosition:UICollectionViewScrollPosition)

Here it’s giving me error at bold position saying:

cannot convert value of type Int to expected argument type 'IndexPath?

I can understand this exception as indexpath is different in swift from collection data index(array index). But I am unable to find any method that can convert item index to indexpath or anyother way.

I am newer to iOS so might be not good at searching correct keywords for such issue. It will be a great favour from you all for any other method to this requirement.

Answer

KrishnaCA picture KrishnaCA · Jan 17, 2017

To create an IndexPath in swift 3 for UICollectionView

let indexPath = IndexPath(item: 0, section: 0)