Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UICollectionView的不可见单元格保持选中状态_Ios_Iphone_Swift_Uicollectionview - Fatal编程技术网

Ios UICollectionView的不可见单元格保持选中状态

Ios UICollectionView的不可见单元格保持选中状态,ios,iphone,swift,uicollectionview,Ios,Iphone,Swift,Uicollectionview,我有一个使用自定义单元格的UICollectionViewController。当用户点击一个单元格时,有一个函数get被调用,以区分所选单元格。我将单元格的背景颜色更改为绿色。 问题是,当用户点击另一个单元格时,应取消选择前一个单元格,并调用另一个函数。只要collectionView没有被滚动,它就可以正常工作,但是当用户滚动collectionView并且选中的一个退出屏幕的可见矩形时,我的取消选择功能就不起作用,并且会有两个绿色背景的单元格 这是演示: 您可以看到顶部有一个绿色背景的单

我有一个使用自定义单元格的UICollectionViewController。当用户点击一个单元格时,有一个函数get被调用,以区分所选单元格。我将单元格的背景颜色更改为绿色。 问题是,当用户点击另一个单元格时,应取消选择前一个单元格,并调用另一个函数。只要collectionView没有被滚动,它就可以正常工作,但是当用户滚动collectionView并且选中的一个退出屏幕的可见矩形时,我的取消选择功能就不起作用,并且会有两个绿色背景的单元格

这是演示:

您可以看到顶部有一个绿色背景的单元格,另一个位于末尾

以下是选择和取消选择单元格的方法:

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    if let cell = collectionView.cellForItem(at: indexPath) as? CategoryCollectionViewCell {
            cell.selectItem()
    }
}

override func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
    for _cell in collectionView.visibleCells {
        if let __cell = _cell as? CategoryCollectionViewCell {
            __cell.deselectItem()
        }
    }

    if let indexPath = collectionView.indexPathsForSelectedItems {
        if indexPath.count > 0 {
            if let _cell = collectionView.cellForItem(at: indexPath.first!) as? CategoryCollectionViewCell {
                _cell.deselectItem()
            }
        }
    }

    return true
}

我想,我明白你的问题了,这只是在滚动滚动滚动视图之后发生的,这意味着选中的单元格变为选中的,并且由于可重用的单元格,另一个单元格自动变为选中的,这是一个常见的问题,主要发生在UITableView和UIScrollView中

在这个视图中,无论您在
数据源
中使用了
if条件
,它的
委托
方法都会放置一个
else
部分,这将解决您的问题

例如:

    if let indexPath = collectionView.indexPathsForSelectedItems {
        if indexPath.count > 0 {
            if let _cell = collectionView.cellForItem(at: indexPath.first!) as? CategoryCollectionViewCell {
                _cell.deselectItem()
            }
        }
       else{
         // do something here
       }
    }
    else{
        // do something here
   }
希望这能对你有所帮助。

试试这个

let selectedIndexPath : IndexPath
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if let cell = collectionView.cellForItem(at: indexPath) as? CategoryCollectionViewCell {
            cell.selectItem()
        }

        if let preViousSelectedcell = collectionView.cellForItem(at: selectedIndexPath) as? CategoryCollectionViewCell {
            preViousSelectedcell.deselectItem()
        }
        selectedIndexPath = indexPath

    }

是多选吗?@KKRocks否,用户应该只选择一个单元格。确定。请尝试我的回答“只要collectionView没有滚动,它就可以正常工作,但当用户滚动collectionView时……”您很可能遇到了问题,因为您正在修改单元格的属性,而不是数据源的属性。滚动collectionView时,单元格将被重复使用。如果不更新数据源,滚动的单元格将不正确。这是相同的。仍然会选择旧的选定单元格。这种方法是正确的。您需要检查是否有正确的上一个单元格。