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 变成红色,原来的一个又变成黑色。”
我必须使用什么功能以及我必须在哪里使用功能。