Scrolling in UICollectionView selects wrongs cells - Swift

我只是一个虾纸丫 提交于 2020-01-11 05:36:05

问题


I have the following UICollectionView which is populated by an Array with NSManagedObject of type Categories

The problem is that when a Cell is selected scrolling does not function correctly. When scrolling through the UICollectionView other cells get selected, and deselected. Strange behaviour. I think this is because of the indexPath that is set incorrectly after scrolling? Anyway, I have been struggling with this for a couple of hours, and cannot seem to grasp it. Hopefully someone can point me in the right direction!

The fetchedCategory gets compared to the category to check if it is already selected and if they are the same 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()
    }
}

回答1:


You don't want to call cellForItemAtIndexPath and configure the cells in the didSelectItemAtIndexPath or didDeselectItemAtIndexPath delegate methods. Also, you shouldn't be calling selectItemAtIndexPath and deselectItemAtIndexPath from within the cellForItemAtIndexPath method.

Instead, just keep track and toggle the state of the selected category in your select/deselect callbacks, and then don't do anything other the set up up the look of your cells in cellForItemAtIndexPath.

As the commenter pointed out, the cells are re-used, so stick to the simple way the delegate callbacks are designed to be used, and you should have much better luck.

If you need to refresh the look of the cells, do it by relying on cellForItemAtIndexPath being called while scrolling and using the reloadData and reloadItemsAtIndexPaths collection view methods if you need to force an update.



来源:https://stackoverflow.com/questions/27363186/scrolling-in-uicollectionview-selects-wrongs-cells-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!