I have a UICollectionView
linked as a @IBOutlet
. Now I want to access any of the cells inside (which are hidden). I.e. to change the color of the cell.
All methods I found need the IndexPath, but where do I get this from?
I have a UICollectionView
linked as a @IBOutlet
. Now I want to access any of the cells inside (which are hidden). I.e. to change the color of the cell.
All methods I found need the IndexPath, but where do I get this from?
There are a couple ways you can create an IndexPath which are all listed on in the Initializers section of IndexPath documentation.
For example if you wanted to access the first item in the first section you could use this initializer:
let index = IndexPath(item: 0, section: 0)
let cell = collectionView.cellForItem(at: index)
Since you're new to Swift, it might also be worthwhile following through a few general examples and tutorials of using UICollectionView
, here's a good starting off point: How to make a simple collection view with Swift.
I guess that you want to access the cells on the collection view. So probably you want your view controller class to inherit from UICollectionViewController and then override the method cellForItemAt:
Something like:
class myViewController: UICollectionViewController, ... {
...
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
...
}
Try to create a new File, then choose cocoa class and then choose that it's a subclass of UICollectionViewController. The methods to override should be there, automatically.