Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get indexpath from row index Swift

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.

like image 925
Saira Nawaz Avatar asked Jan 17 '17 10:01

Saira Nawaz


People also ask

How do I get indexPath in Swift?

add an 'indexPath` property to the custom table cell. initialize it in cellForRowAtIndexPath. move the tap handler from the view controller to the cell implementation. use the delegation pattern to notify the view controller about the tap event, passing the index path.

What does indexPath row return?

So to start with the indexPath. row will be 0. Then it will be 1, then 2, then 3 and so on. You do this so that you can get the correct string from the array each time.

What is indexPath row in Swift?

A list of indexes that together represent the path to a specific location in a tree of nested arrays.


1 Answers

To create an IndexPath in swift 3 for UICollectionView

let indexPath = IndexPath(item: 0, section: 0)
like image 198
KrishnaCA Avatar answered Oct 23 '22 07:10

KrishnaCA