选中时更改uicollectionviewcell背景,同时将其他单元格背景变为透明

时间:2018-10-03 00:05:18

标签: swift uicollectionview uicollectionviewcell

在这个问题上我需要帮助。我希望我的应用程序将其某个单元格的背景转换为UIColor.blue,但我也希望其将所有其他单元格的背景转换为清晰的颜色。基本上,我希望单击的单元格在其余单元中脱颖而出,向用户表明他们只是单击该单元格。

因此,下面将单元格用户单击的背景变为蓝色。但是一旦我单击其他单元格,当前当前单元格也是蓝色的。当用户单击单元格时,我需要它,它将其他单元格变回清晰的背景。

 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let currentCell:dateCollectionCell = collectionViews.cellForItem(at: indexPath) as! dateCollectionCell
        currentCell.label.backgroundColor = .blue
    }

//这是整个代码

class hello: UIView,UICollectionViewDelegateFlowLayout,UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 15
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let currentCell:dateCollectionCell = collectionViews.cellForItem(at: indexPath) as! dateCollectionCell
        currentCell.label.backgroundColor = .blue
    }

    lazy var collectionViews: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.sectionInset = UIEdgeInsetsMake(5,5,5,5)
        layout.scrollDirection = UICollectionViewScrollDirection.vertical
        let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
        cv.showsVerticalScrollIndicator = false
        cv.backgroundColor = UIColor.clear
        cv.dataSource = self
        cv.delegate = self
        return cv
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
    }

    func setupViews(){
        backgroundColor = .red
        collectionViews.register(warningCollectionCell.self, forCellWithReuseIdentifier: "cell")
        collectionViews.translatesAutoresizingMaskIntoConstraints = false
        addSubview(collectionViews)
        collectionViews.topAnchor.constraint(equalTo: topAnchor, constant: 0).isActive = true
        collectionViews.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 0).isActive = true
        collectionViews.centerYAnchor.constraint(equalTo: centerYAnchor, constant: 0).isActive = true
        collectionViews.centerXAnchor.constraint(equalTo: centerXAnchor, constant: 0).isActive = true
        collectionViews.widthAnchor.constraint(equalTo: widthAnchor, constant: 0).isActive = true
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

1 个答案:

答案 0 :(得分:0)

您只需在$Pwd = 'certificatepassword' $Pfx = New-Object -TypeName 'System.Security.Cryptography.X509Certificates.X509Certificate2' $Pfx.Import('clientcert.p12', $Pwd, 'Exportable,PersistKeySet') Invoke-RestMethod -Uri 'https://your.rest.host/api/' -Certificate $Pfx -OtherParam ... 函数中更改背景颜色。但是首先,您需要跟踪要变成蓝色的行索引:

cellForItemAt

然后在var selectedCellIndex: Int = -1 函数中

cellForItemAt