0

I am having problems when I do a ReloadData to a CollectionView.

The thing is that I have done a very small project in order to show you what's happening.

(The CollectionView will change the colour background of the cell number 2..but when you do a ReloadData, many cells changed..)

You can find my project here: https://www.dropbox.com/s/yy7uen60rc292ir/collectionViewTest.zip?dl=0

Solved!

Thanks for your help!, The thing is I needed to add the function PrepareForReuse() where I can "clean" my cells before being reused.

LPS
  • 581
  • 3
  • 8
  • 17

1 Answers1

1

reloadData() reloads the entire datasource for the collectionView. If you are looking to only update a few cells then look into beginUpdates() and endUpdates(). Here you will be inserting or deleting rows. If you're using Core Data then look into using NSFetchedResultsController as this will simplify the entire process and create smooth animations when your data changes.

When you use dequeueReusableCellWithIdentifier you are reusing those cells. So if you change a background color on one then reuse it you'll see inconsistent changes when reloading the table. Make sure to set the background color on all cells or you'll experience this.

    var cell = tableView.dequeueReusableCellWithIdentifier("SomeCell") as! UICollectionViewCell
 // This would alternate the colors just to show you what I mean
    indexPath.row % 2 == 0 ? (cell.backgroundColor = UIColor.whiteColor()) : (cell.backgroundColor = UIColor.blackColor())
Mark McCorkle
  • 9,349
  • 2
  • 32
  • 42
  • Thanks for your answer!, The thing is i dont' understand why if I am changing the colour background of a cell ONLY when indexpath.row == 2...Why If I do a RealoadData() another cells changed their colours? – LPS Apr 16 '15 at 14:40
  • Are you reusing cells? Did you set the background on another? You'll need to set the background for both when reusing cells. – Mark McCorkle Apr 16 '15 at 15:01
  • Ok now I understand...the app is reusing my cell in another position so if i have changed its background, then i will see it in another place....I will try to fix it with that info! Thanks! – LPS Apr 16 '15 at 15:40