0

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?

Cœur
  • 32,421
  • 21
  • 173
  • 232
loki
  • 122
  • 1
  • 8
  • 3
    `Now I want to access any of the cells inside (which are hidden). I.e. to change the color of the cell.` Why do you want to change the color of something that is hidden? – Larme Apr 10 '17 at 14:40
  • @loki what you are try? – Yogendra Girase Apr 10 '17 at 14:47
  • Don't change anything in the view (the cell) directly. Add a property to your model (the data source array), change that and reload the collection view. In `cellForItem` set the color according to the property's state. – vadian Apr 10 '17 at 14:50
  • Welcome to StackOverflow :) When you're asking a question, it helps to include what you've attempted so far (either code or research), which will help improve the chance of getting a good answer. Please take a look at https://stackoverflow.com/help/how-to-ask when you ask your next question – MathewS Apr 10 '17 at 14:57
  • @Larme: So it has a new color, when it gets visible again. Which can be later. – loki Apr 11 '17 at 11:03
  • @YogendraGirase: What should happen is, that once i tap a button. A random tile of the CollectionView gets unhidden. – loki Apr 11 '17 at 11:18

2 Answers2

0

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.

Community
  • 1
  • 1
MathewS
  • 2,187
  • 2
  • 17
  • 30
  • 1
    Carefull: Is by "hidden" the author means not present on the screen, `cell` should be `nil` and won't be able to set anything. – Larme Apr 10 '17 at 16:04
  • @Larme interesting! _The cell object at the corresponding index path or nil if the cell is not visible or index​Path is out of range._ ... I learnt something new :) – MathewS Apr 10 '17 at 18:57
  • It's normal if you understood correctly the reuse of the cells. The cell you call that is out of screen may have been reused and serves currently for another indexPath. – Larme Apr 11 '17 at 07:58
  • does that mean, that hidden cells have no IndexPath ? – loki Apr 11 '17 at 11:19
0

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.

José Fonte
  • 3,711
  • 2
  • 15
  • 26
  • well, I meant inside the UIViewController I have a @IBOutlet weak var that is linked to a Collection View. I never said anything about a ControllerView. – loki Apr 11 '17 at 10:59
  • Maybe was a typo, as your question was edited. Thanks for clarification. – José Fonte Apr 11 '17 at 11:01