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() } }
source share