我想做这样的事情。
(1)。如果我按下31中的那个按钮(A)之一,那个数字(A)就会变成红色。
(2)。然后如果我按下另一个按钮(B),然后(A)再次变黑,然后(B)变红。
**
class ViewController: UICollectionViewController {
var collectionViewCell = CollectionViewCell()
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()
}
}
答案 0 :(得分:0)
您必须将所选单元格的行号保存在视图控制器中。
添加callback
闭包而不是修改单元格中的颜色。在cellForRow
中,通过重新加载行来处理回调,以将当前行设置为红色,将上一行设置为黑色。
类似这样的东西
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.textColor = indexPath.item == selectedRow ? .red : .black
cell.callback = { [unowned self] in
if let row = self.selectedRow {
self.selectedRow = indexPath.item
collectionView.reloadItems(at: [IndexPath(item: row, section: 0)])
}
cell.Label.textColor = .red
}
cell.Label.text = String(data[indexPath.row])
return cell
}
}
class CollectionViewCell: UICollectionViewCell {
@IBOutlet weak var Label: UILabel!
@IBOutlet weak var button: UIButton!
var callback : (() -> Void)?
@IBAction func buttonPressed(_ sender: UIButton) {
callback?()
}
}
答案 1 :(得分:0)
您必须使用isSelected
和func setSelected(_ selected: Bool, animated: Bool) {
但是首先您需要知道,在Swift中,我们对变量使用了camelCase样式。
现在,我们将不得不更改您的单元格实现:
class CollectionViewCell: UICollectionViewCell {
@IBOutlet weak var label: UILabel!
@IBOutlet weak var button: UIButton!
public override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
if selected {
label.textColor = .red
} else {
label.textColor = .black
}
}
@IBAction func buttonPressed(_ sender: UIButton) {
self.isSelected = !self.isSelected
}
}
也不要忘记将cell.Label.text = String(data[indexPath.row])
更新为cell.label.text = String(data[indexPath.row])
编辑
Leo Darbus指出,使用isSelected.toggle()
比self.isSelected = !self.isSelected
更好,因为它已经由系统提供