【问题标题】:How to resolve UIButton border issue after reload collection view重新加载集合视图后如何解决 UIButton 边框问题
【发布时间】:2020-02-27 12:01:58
【问题描述】:

我必须为 UIButton 应用边框,它的颜色与 UIButton 背景颜色匹配,它工作正常,但是当我重新加载集合视图时,它适用于其他按钮,然后重新加载它适用于其他按钮。假设如果集合视图重新加载 3 次,那么它将边框应用于 3 个错误的按钮,它应该只应用那些背景颜色与颜色变量颜色匹配的按钮

注意:- 一次只有一个按钮可以有边框。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ChromazonColorCell", for: indexPath) as! ChromazonColorCell
    cell.configureData(data: colorSource[indexPath.row])
    cell.cellColorButton.tag = indexPath.row
    if color == colorSource[indexPath.row] {
        cell.cellColorButton.layer.cornerRadius = 5.0
        cell.cellColorButton.clipsToBounds = true
        cell.cellColorButton.layer.borderWidth = 2
        cell.cellColorButton.layer.borderColor = UIColor.black.cgColor
    }
    return cell
}

【问题讨论】:

  • 永远不要只在我们必须提供其他的情况下才这样做。

标签: ios uicollectionview swift4 uicollectionviewcell


【解决方案1】:

Dixit Rathod 已经回答了。

而且,您可以在您的单元格中使用您的代码和prepareForReuse 方法

func prepareForReuse(){ 
    super.prepareForReuse()

    cellColorButton.layer.cornerRadius = 0.0
    cellColorButton.clipsToBounds = true
    cellColorButton.layer.borderWidth = 0
    cellColorButton.layer.borderColor = UIColor.clear.cgColor
}

【讨论】:

    【解决方案2】:

    我认为这应该对您有用.. 当您重新加载集合视图时,它将重用您的单元格。并且您重复使用的单元格将具有边框,因此它也适用于其他单元格。 请尝试以下解决方案

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ChromazonColorCell", for: indexPath) as! ChromazonColorCell
      cell.configureData(data: colorSource[indexPath.row])
      cell.cellColorButton.tag = indexPath.row
      if color == colorSource[indexPath.row] {
        cell.cellColorButton.layer.cornerRadius = 5.0
        cell.cellColorButton.clipsToBounds = true
        cell.cellColorButton.layer.borderWidth = 2
        cell.cellColorButton.layer.borderColor = UIColor.black.cgColor
      }
    else{
        cell.cellColorButton.layer.cornerRadius = 5.0
        cell.cellColorButton.clipsToBounds = true
        cell.cellColorButton.layer.borderWidth = 0
        cell.cellColorButton.layer.borderColor = UIColor.clear.cgColor
    }
     return cell
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多