gpt4 book ai didi

arrays - 我如何引用数组中的 UILabel 或 UIButton 集合 indexPath

转载 作者:行者123 更新时间:2023-11-30 11:38:30 27 4
gpt4 key购买 nike

@IBOutlet var buttons: [UIButton]!
@IBOutlet var labels: [UILabel]!

@IBAction func cevapSeçildi(_ sender: UIButton) {
if sender == buttons[0] {
`enter code here`
labels[0].backgroundColor = UIColor.yellow
}
}

我想要这个..

var x : Int

if sender == buttons[x] { labels[x].backgroundColor = UIColor.yellow }

你能帮我吗

最佳答案

几点:

  1. 使用按钮数组映射到单元格索引仅适用于单节 TableView 或 Collection View 。如果您有行和列中的分段 TableView 或 Collection View ,则该方法将不起作用。

  2. 如果您想将所选单元格的标签设置为黄色,而将所有其他单元格的标签设置为白色,则更改所有单元格是没有意义的。 TableView / Collection View 一次仅显示几个单元格,当您滚动时,这些单元格将被回收并用于 TableView / Collection View 中的不同索引。

如果您让我知道您使用的是 TableView 还是 Collection View ,我可以向您展示更好的方法。

编辑:

由于您没有使用 TableView 或 Collection View ,因此直接操作标签确实有意义:

@IBAction func cevapSeçildi(_ sender: UIButton) {
let buttonIndex = buttons.index(of: sender)
for (index, label) in labels.enumerated) {
}
if index == buttonIndex {
label.backgroundColor = UIColor.yellow
} else {
label.backgroundColor = UIColor.white
}
}

关于arrays - 我如何引用数组中的 UILabel 或 UIButton 集合 indexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49470635/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com