为什么CollectionView中的自定义按钮仅加载到最后一个单元格? (迅速)

时间:2018-08-06 06:24:03

标签: ios swift uicollectionview swift4

为什么我在 CollectionView 中的自定义按钮仅加载到最后一个单元格

如何在所有单元格中加载该按钮?

var editButton = UIButton()

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

    switch ColorSegment.selectedSegmentIndex {
      case 0:
        cell.CardView.backgroundColor = ColorArray[indexPath.row]
        cell.ColorName.text = ColorName[indexPath.row]
      case 1:
        cell.CardView.backgroundColor = CustomColorArray[indexPath.row]
        cell.ColorName.text = CustomColorName[indexPath.row]

        editButton.frame = CGRect(x:63, y:0, width:20,height:20)
        editButton.layer.cornerRadius = 10
        editButton.backgroundColor = UIColor.lightGray
        editButton.layer.setValue(indexPath.item, forKey: "index")
        editButton.setImage(UIImage(named: "CloseScan"), for: UIControlState.normal)
        editButton.addTarget(self, action: #selector(deleteCell), for: UIControlEvents.touchUpInside)
        cell.addSubview(editButton)
      default: print("error with switch statement for cell data")
    }
    return cell
}

编辑按钮功能

     if EditButton.currentTitle == "Edit" {
            EditButton.setTitle("Cancel", for: .normal)
            //DeleteButton.isHidden = false
     } else if EditButton.currentTitle == "Cancel" {
            EditButton.setTitle("Edit", for: .normal)
            //DeleteButton.isHidden = true
     }

3 个答案:

答案 0 :(得分:2)

问题是您要在所有单元格中添加相同的editButton,您需要为所有单元格创建新的UIButton

因此更改行

editButton.frame = CGRect(x:63, y:0, width:20,height:20)

使用

//Create new button instance every time
let editButton = UIButton(frame: CGRect(x:63, y:0, width:20,height:20))

此外,cellForItemAt将重复使用该单元格,因此您不必每次都尝试像这样创建新的UIButton

let editButton: UIButton 
if let btn = cell.viewWithTag(101) as? UIButton {
    editButton = btn
}
else  {
    editButton = UIButton(frame: CGRect(x:63, y:0, width:20,height:20))
}
editButton.layer.cornerRadius = 10
editButton.backgroundColor = UIColor.lightGray
editButton.layer.setValue(indexPath.item, forKey: "index")
editButton.setImage(UIImage(named: "CloseScan"), for: UIControlState.normal)
editButton.addTarget(self, action: #selector(deleteCell), for: UIControlEvents.touchUpInside)
//Set tag for reuse
editButton.tag = 101
cell.addSubview(editButton)

您可以像这样在按钮操作中获取单元格的indexPath。

@objc func deleteCell(_ sender: UIButton) {
    let point = sender.superview?.convert(sender.center, to: self.collectionView) ?? .zero
    guard let indexPath = self.collectionView.indexPathForItem(at: point) else { return }
    //Use indexPath here
}

编辑:如果您想隐藏或显示按钮要比隐藏按钮好,您只需在设计中添加按钮,然后在cellForItemAt中的CustomCollectionViewCell中创建一个插座,然后隐藏即可并根据情况显示按钮。如果段的选定索引为1,则需要从代码中显示Delete按钮。因此,使您的cellForItem像这样。

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

    cell.CardView.backgroundColor = ColorArray[indexPath.row]
    cell.ColorName.text = ColorName[indexPath.row]
    //Add delete button in the xib or storyboard collectionViewCell
    //Now hide this button if EditButton title is not "Edit"
    cell.editButton.isHidden = EditButton.currentTitle != "Edit"
    //Add action of button
    cell.editButton.addTarget(self, action: #selector(deleteCell), for: UIControlEvents.touchUpInside)
    return cell
}

在那之后,在您的“编辑”按钮操作中,设置了按钮标题后,重新加载collectionView。

if EditButton.currentTitle == "Edit" {
    EditButton.setTitle("Cancel", for: .normal)
} else if EditButton.currentTitle == "Cancel" {
    EditButton.setTitle("Edit", for: .normal)
}
//Reload your collectionView
self.collectionView.reloadData()

答案 1 :(得分:1)

我认为您可以移动这行:

var editButton = UIButton()

要功能 cellForItemAt

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

    switch ColorSegment.selectedSegmentIndex {
      case 0:
        cell.CardView.backgroundColor = ColorArray[indexPath.row]
        cell.ColorName.text = ColorName[indexPath.row]
      case 1:
        cell.CardView.backgroundColor = CustomColorArray[indexPath.row]
        cell.ColorName.text = CustomColorName[indexPath.row]

        // create editButton in here
        var editButton = UIButton()

        editButton.frame = CGRect(x:63, y:0, width:20,height:20)
        editButton.layer.cornerRadius = 10
        editButton.backgroundColor = UIColor.lightGray
        editButton.layer.setValue(indexPath.item, forKey: "index")
        editButton.setImage(UIImage(named: "CloseScan"), for: UIControlState.normal)
        editButton.addTarget(self, action: #selector(deleteCell), for: UIControlEvents.touchUpInside)
        cell.addSubview(editButton)

      default: print("error with switch statement for cell data")
    }
    return cell
}

答案 2 :(得分:1)

像这样在switch语句之外移动按钮逻辑

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

switch ColorSegment.selectedSegmentIndex {
  case 0:
    cell.CardView.backgroundColor = ColorArray[indexPath.row]
    cell.ColorName.text = ColorName[indexPath.row]
  case 1:
    cell.CardView.backgroundColor = CustomColorArray[indexPath.row]
    cell.ColorName.text = CustomColorName[indexPath.row]

  default: print("error with switch statement for cell data")
}
    var editButton = UIButton.init(frame: CGRect.init(x: 63, y: 0, width: 20, height: 20))
    editButton.layer.cornerRadius = 10
    editButton.backgroundColor = UIColor.lightGray
    editButton.layer.setValue(indexPath.item, forKey: "index")
    editButton.setImage(UIImage(named: "CloseScan"), for: UIControlState.normal)
    editButton.addTarget(self, action: #selector(deleteCell), for: UIControlEvents.touchUpInside)
    cell.addSubview(editButton)
return cell

}

相关问题