Scrolling in UICollectionView selects cells with errors - Swift

I have the following UICollectionView that is populated with an Array with an NSManagedObject type Categories

The problem is that when you select Cell scrolling does not work properly. When scrolling UICollectionView page UICollectionView other cells are selected and canceled. Strange behavior. I think this is due to indexPath, which is set incorrectly after scrolling? In any case, I struggled with this for several hours and did not seem to understand it. Hope someone can point me in the right direction!

FetchedCategory is compared with a category to check if it is already selected, and if they match, the colors are inverted.

 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { var cell = collectionView.dequeueReusableCellWithReuseIdentifier("CategorySelectionCollectionCell", forIndexPath: indexPath) as CategoryCollectionViewCell if fetchedCategories[indexPath.row] == category { cell.categoryLabel?.text = fetchedCategories[indexPath.row].name cell.categoryLabel?.textColor = UIColor.whiteColor() cell.backgroundColor = fetchedCategories[indexPath.row].iconColor as? UIColor collectionView.selectItemAtIndexPath(indexPath, animated: true, scrollPosition: UICollectionViewScrollPosition.None) } else { cell.categoryLabel?.text = fetchedCategories[indexPath.row].name cell.categoryLabel?.textColor = fetchedCategories[indexPath.row].iconColor as UIColor collectionView.deselectItemAtIndexPath(indexPath, animated: true) } return cell } func collectionView(collectionView: UICollectionView!, didSelectItemAtIndexPath indexPath: NSIndexPath!) { var cell = collectionView.cellForItemAtIndexPath(indexPath) as CategoryCollectionViewCell cell.categoryLabel?.textColor = UIColor.whiteColor() cell.backgroundColor = fetchedCategories[indexPath.row].iconColor as? UIColor category = fetchedCategories[indexPath.row] } func collectionView(collectionView: UICollectionView!, didDeselectItemAtIndexPath indexPath: NSIndexPath!) { if var cell = collectionView.cellForItemAtIndexPath(indexPath) as? CategoryCollectionViewCell { cell.categoryLabel?.text = fetchedCategories[indexPath.row].name cell.categoryLabel?.textColor = fetchedCategories[indexPath.row].iconColor as UIColor cell.backgroundColor = UIColor.whiteColor() } } 
+5
source share
1 answer

You do not want to call cellForItemAtIndexPath and configure cells in the didSelectItemAtIndexPath or didDeselectItemAtIndexPath delegation methods. In addition, you should not call selectItemAtIndexPath and deselectItemAtIndexPath from the cellForItemAtIndexPath method.

Instead, just hold the track and switch the state of the selected category to your select / deselect calls, and then do nothing else to customize the appearance of your cells in cellForItemAtIndexPath.

As the commentator noted, cells are reused, so stick with a simple way to handle delegate callbacks, and you should be much better.

If you need to update the appearance of the cells, do this by relying on the called cellForItemAtIndexPath while scrolling and using the reloadData and reloadItemsAtIndexPaths collection lookup methods if you need to force a refresh.

+4
source

Source: https://habr.com/ru/post/1208596/


All Articles