3
class ViewController: UICollectionViewController {

var selectedRow : Int?

let data = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31]

override func viewDidLoad() {
    super.viewDidLoad()
    collectionView.register(UINib(nibName: "CollectionViewCell", bundle: nil), forCellWithReuseIdentifier : "MyCell")
}

override func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return data.count
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath) as! CollectionViewCell
    cell.Label.text = String(data[indexPath.row])
    return cell
 }}



class CollectionViewCell: UICollectionViewCell {
  @IBOutlet weak var Label: UILabel!


@IBOutlet weak var button: UIButton!
@IBAction func buttonPressed(_ sender: UIButton) {
  if Label.textColor == .red{
   Label.textColor = .black
  } else if Label.textColor == .black{
   Label.textColor = .red
  }}


public override func awakeFromNib() {
  super.awakeFromNib()}}

我想识别是否选择了单元格。但我真的不知道我必须在哪里使用什么功能。很多人都在说使用 setSelected 函数,但我认为没有这样的函数。我是初学者所以不太清楚。我想要做的是“如果我选择其中一个数字,那么那个单元格的 textColor 变成红色。然后我选择另一个单元格。然后那个单元格的 textColor 变成红色,原来的一个又变成黑色。”

我必须使用什么功能以及我必须在哪里使用功能。

4

2 回答 2

1

有很多方法可以做到,我更喜欢下面的方法。

变化:1

您需要将buttonPressed方法添加到UIViewController

变化:2

您需要将 UILabel 文本颜色的代码添加到cellForItemAt.

完整代码:

class ViewController: UICollectionViewController {
    
    var selectedRow : Int?
    
    let data = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView.register(UINib(nibName: "CollectionViewCell", bundle: nil), forCellWithReuseIdentifier : "MyCell")
    }
    
    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return data.count
    }
    
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath) as! CollectionViewCell
        
        cell.button.tag = indexPath.item
        cell.button.addTarget(self, action:#selector(buttonPressed(_:)) , for: .touchUpInside)
        cell.Label.text = String(data[indexPath.row])
        
        if selectedRow == indexPath.item{
            cell.Label.textColor = .black
        }else{
            cell.Label.textColor = .red
        }
        return cell
    }
    
    @objc func buttonPressed(_ sender: UIButton) {
        if selectedRow == sender.tag{
            selectedRow = nil
        }else{
            selectedRow = sender.tag
        }
        collectionView.reloadData()
    }
}
于 2020-04-21T08:33:25.293 回答
0

您应该在 ViewController 类中重载 UICollectionViewDelegate 的collectionView(_:didSelectItemAt:)方法。每当用户点击一个单元格时,它就会被调用。

于 2020-04-21T08:38:54.380 回答