我们如何在 select 项目委托上重新加载 collection 视图?

How do we reload a collection view on its did select item delegate?

我无法从 Select 项目重新加载我的 collection 视图。当我重新加载 collection 视图时,委托 numberOfItemsInSectionnumberOfSections 被调用。但是,cellForItemAt 不会被调用。为什么是这样? .从它自己的 didSelectItemAt

重新加载 collection 视图是否不合适

我的代码如下

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

    indexSelectedCell = indexPath.row

    collectionView.performBatchUpdates({

        DispatchQueue.main.async(execute: {
            self.collAvailableLanguages.reloadData()
        })

    }, completion: nil)
}

我的要求是我想突出显示所选单元格中的标签并取消突出显示前一个单元格。我通过跟踪当前选定的单元格索引来(尝试)它。我的密码是

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "LanguageCell", for: indexPath) as? BaashaLanguageCell {
        cell.lblLanStr.text = arrAvailbleLanDemo[indexPath.row]

        if indexPath.row != 0 || indexPath.row != arrAvailbleLanDemo.count - 1 {
            cell.layer.borderWidth = 0.5
        }

        if indexSelectedCell == indexPath.row {
            print("OK")
            cell.layer.borderColor = UIColor.clear.cgColor

            switch isHostLanVC {
            case true:
                cell.lblLanStr.textColor = UIColor(rgb: 0x599441)
            default:
                cell.lblLanStr.textColor = UIColor(rgb: 0x5F90CB)

            }
        } else {
            cell.layer.borderColor = UIColor.lightGray.cgColor
        }

        return cell
    } else {
        return BaashaLanguageCell()
    }
}

我明白你在做什么,也知道你为什么这样做,但这有点矫枉过正,因为你无需重新加载就可以实现所有这些,而且都在你的 didSelect 方法中。您还需要在单元格 class 中选择状态变量,如下所示:

var isSelected: Bool = false 

//

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

    if let cell = self.collectionView.cellForItem(at: indexPath) as? Baasha_LanguageCell {

        if indexPath.item == 0 {

            if cell.isSelected == true {
                // SELECTING CELL
                cell.backgroundColor = .red
            } else {
                // DESELECTING CELL
                cell.backgroundColor = .white
            }

        }

    }
}

方法 reloadItems(at:) 应该可以满足您的需求。

class MyCollectionViewController: UICollectionViewController {

    var selectedIndexPath: IndexPath? // Save the whole index path. It's easier.

    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        var reloadIndexPaths = [indexPath]

        // If an cell is already selected, then it needs to be deselected.
        // Add its index path to the array of index paths to be reloaded.
        if let deselectIndexPath = selectedIndexPath { reloadIndexPaths.append(deselectIndexPath) }

        selectedIndexPath = indexPath

        collectionView.reloadItems(at: reloadIndexPaths)
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "LanguageCell", for: indexPath) as? Baasha_LanguageCell else {
            return Baasha_LanguageCell()
        }

        if indexPath == selectedIndexPath {
            // yes
        } else {
            // no
        }

        return cell
    }

}