【问题标题】:CollectionView First And Last Cell remains selectedCollectionView 第一个和最后一个单元格保持选中状态
【发布时间】:2021-02-10 18:44:47
【问题描述】:

我正在使用一个可选择的 collectionView,当用户选择第一个然后最后一个单元格时,两个单元格由于某种原因仍然处于选中状态,不知道为什么。

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
    if (collectionView == self.categoryCollectionView)
    {
        let cell = collectionView.cellForItem(at: indexPath) as? ChannelsCategoryCell
        cell?.triangleIndicatorView.isHidden = false
        cell?.categoryLabel.textColor = UIColor.init(named: "ActiveSymbol")
        
        loadChannelList()
    }

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath)
{
    if (collectionView == self.categoryCollectionView)
    {
        let cell = collectionView.cellForItem(at: indexPath) as? ChannelsCategoryCell
    
        cell?.triangleIndicatorView.isHidden = true
        cell?.categoryLabel.textColor = UIColor.init(named: "InactiveCategoryText")
    }

【问题讨论】:

    标签: swift xcode uicollectionview


    【解决方案1】:
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
       let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as! CollectionViewCell
        if selectedIndex == indexPath.item {
          cell.backgroundColor = .blue
        } else {
          cell.backgroundColor = .clear
        }
     }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        selectedIndex = indexPath.item
        collectionView.reloadData()
    }
    

    【讨论】:

    • 背景颜色与单元格选择无关
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-16
    • 1970-01-01
    • 2016-02-14
    • 1970-01-01
    • 1970-01-01
    • 2018-11-25
    • 1970-01-01
    相关资源
    最近更新 更多