更改选定集合视图单元格的边框颜色不起作用

时间:2019-02-09 00:19:41

标签: uicollectionview border uicollectionviewcell highlight border-color

选择和取消选择有效,但是边框的颜色和粗细没有改变。我还附上了一张cell.xib图片。

extension CharactersViewController: UICollectionViewDataSource {

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.characters.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! cell
    cell.setData(text: self.characters[indexPath.row])
    return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! cell
    let character = characters[indexPath.row]

    onBoardingService.pickCharacter(character: character)
    cell.layer.borderWidth = 1.0
    cell.layer.borderColor = UIColor.gray.cgColor
}

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! cell
    let character = characters[indexPath.row]

    onBoardingService.removeCharacter(character: character)
    cell.layer.borderWidth = 5.0
    cell.layer.borderColor = UIColor.red.cgColor
}
}

cellxib

(此代码会影响边框的颜色/宽度吗?)

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    self.setupGridView()
    DispatchQueue.main.async {
        self.collectionView.reloadData()
    }
}

func setupGridView() {
    let flow = collectionView?.collectionViewLayout as! UICollectionViewFlowLayout
    flow.minimumInteritemSpacing = CGFloat(self.cellMarginSize)
    flow.minimumLineSpacing = CGFloat(self.cellMarginSize)
}

0 个答案:

没有答案