0

I have a collection view, but cell that are not appearing in the screen i cannot access, 1...5 ok, and those at index parth row 6, 7, and they have to be scrolled to bee seen

func Action0(sender: UIButton!) {
    for i in 0...7 {
        if i != 0{
        var indexPath = NSIndexPath(forRow: i, inSection: 0)
        var cell = collectionView!.cellForItemAtIndexPath(indexPath)
        cell!.backgroundColor = UIColor.whiteColor()
        }else{
        var indexPath = NSIndexPath(forRow: 0, inSection: 0)
        var cell = collectionView!.cellForItemAtIndexPath(indexPath)
        cell!.backgroundColor = UIColor.blueColor()
        }
    }
}

How to acces those cells that are not yet visible?

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath) as! CollectionViewCell
    cell.backgroundColor = UIColor.blackColor()
    cell.textLabel.text = "\(indexPath.section):\(indexPath.row)"
    cell.buttonView.setBackgroundImage(UIImage(named: "Good"), forState: UIControlState.Normal)
    cell.buttonView.addTarget(self, action: Selector("Action\(indexPath.row):"), forControlEvents: UIControlEvents.TouchUpInside)
    return cell
}
Josip Bogdan
  • 578
  • 1
  • 5
  • 15

1 Answers1

3

Cells that are not yet visible do not exist. Table views and collection views only create just the cells that are currently visible. When you scroll, the cells that go out of view get recycled and reused for the new index path(s) that come into view.

You need to think in terms of your data model, which holds the data for each index path, and cells, which display a view of of subset of some of that data.

Duncan C
  • 128,072
  • 22
  • 173
  • 272