簡體   English   中英

為什么以編程方式選擇單元格后 - 單元格不是交互?

[英]Why after selecting cell programmatically - cell is not interaction?

我在以編程方式選擇的 collectionView 中設置了一個單元格:

  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(with: FAFavouriteCategoryCell.self, for: indexPath)!
        let currentCategory = categories[indexPath.row]
        cell.isSelected = selectedCategories.contains(currentCategory.categoryID)
        cell.configCell(category: currentCategory)
        return cell
    }

在此之后所有選定的單元格都不起作用。 我無法點擊它。 如果我單擊方法shouldDeselectItemAt不調用

您需要像這樣調用selectItem

  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(with: FAFavouriteCategoryCell.self, for: indexPath)!
        let currentCategory = categories[indexPath.row]
        if selectedCategories.contains(currentCategory.categoryID) {
            collectionView.selectItem(at: indexPath, animated: false, scrollPosition: .left) //Add this line
            cell.isSelected = true
        } else {
            collectionView.deselectItem(at: indexPath, animated: false)
            cell.isSelected = false
        }
        cell.configCell(category: currentCategory)
        return cell
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM