I need the index of a cell of my UICollectionView
. I know how get it, but the problem is when I convert this NSIndexpath
to Int
. This is my code.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let cell = sender as! UICollectionViewCell
let index = self.collectionView!.indexPathForCell(cell)!
let vc : VideosViewController = segue.destinationViewController as! VideosViewController
vc.id_category = self.result_category[index.row as Int]["id"].intvalue
}
A variable can be declared as String type by following the below syntax, Swift provides the function of integer initializers using which we can convert a string into an Int type. To handle non-numeric strings, we can use nil coalescing using which the integer initializer returns an optional integer.
In Swift, an indexPath is a list of indexes that, together, represent the path to a specific location in a tree of nested arrays. It describes an item’s position inside a table view or collection view, storing both its section and its position inside that section.
NSIndexPath has a read-only structure that contains two Int property sections and rows if you're working with UITableViews or a section and item if you're working with UICollectionViews. And read them by accessing their properties: It's as easy as that!
Sponsor sarunw.com and reach thousands of iOS developers. Int has an initializer that creates a new integer value from the given string. To convert a string to integer, we initialize an Int with desired string. Before you go with these methods, you should know which kind of strings that Int can convert to integer.
You don't need to convert the index.row
to Int
, it's a Int
already, see the declaration of Apple:
var row: Int { get }
In the class NSIndexPath
.
Just do vc.id_category = self.result_category[index.row]["id"].intvalue
. Just be careful with the index of out bound
in the array.
I hope this help you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With