当不在屏幕上时,如何更改collectionviewcell的属性?

时间:2020-06-22 11:44:17

标签: swift uicollectionview uicollectionviewcell

我使用collectionview创建菜单,但是该菜单无法在屏幕上一次显示所有项目。 例如,有9个单元格,但只能同时显示5个单元格。

当我向下滚动到底部并且前4个不能在屏幕上显示时,但是现在我必须更改第一个单元格的属性,它将导致应用程序崩溃,该怎么办?

 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        switch collectionView.tag{
        case 0:
            if selectedMenuItem != indexPath.row{//selectedMenuItem is an Int object to save the row of selectedMenuItem
                let selectedCell = menuCollectionView.cellForItem(at: indexPath)! as! CollectionViewCellMenu
                selectedCell.bgView.backgroundColor = UIColor(red: 163.0/255.0, green: 129.0/255.0, blue: 89.0/255.0, alpha: 0.2)
                selectedCell.img.image = UIImage(named: selectedMenu[indexPath.row])
                
                //change the previous cell
                let deselectedCell = menuCollectionView.cellForItem(at: IndexPath(row: selectedMenuItem, section: 0))! as! CollectionViewCellMenu
                deselectedCell.bgView.layer.sublayers?.removeAll()
                deselectedCell.img.image = UIImage(named: menu[selectedMenuItem])
                selectedMenuItem = indexPath.row
            }
        case 1:
            print("you select \(indexPath.row) ")
        default: break
        }
    }

0 个答案:

没有答案