gpt4 book ai didi

ios - 动态创建 UILabel 并同时更改文本

转载 作者:行者123 更新时间:2023-11-29 00:58:23 24 4
gpt4 key购买 nike

我正在尝试以编程方式创建 7 个 UILabel,但我无法更改所有 UILabel 的文本。

let viewsCount = 2
let viewLblcount = 7
var messageForLbl = ["helo", "bhago", "futo", "bhensali" , "bhaaw bhaaw", "haha" ,"alladh"]

let floatValue = CGFloat(viewsCount)
let floatLbl = CGFloat(viewLblcount)

let scrollView: UIScrollView = UIScrollView(frame: CGRectMake(20, 30, 320, 250 * floatValue))

for i in 0 ..< viewsCount {
let views: UIView = UIView(frame: CGRectMake(0, 320 * CGFloat(i) + floatValue, self.view.frame.size.width - 40, 300))
views.backgroundColor = UIColor.grayColor()
views.tag = i + 1

for j in 0 ..< viewLblcount {
let label = UILabel(frame: CGRectMake(20, 40 * CGFloat(j) + floatLbl, self.view.frame.size.width - 100, 20))
let label2 = UILabel(frame: CGRectMake(10, 90 * CGFloat(j) + floatLbl, self.view.frame.size.width - 100, 30))

label.backgroundColor = UIColor.cyanColor()
label.text = "iOS LABEL TRY"

label2.backgroundColor = UIColor.magentaColor()
label2.text = "iOS LABEL TRY2"

label.tag = j + 1
label2.tag = j + 1

views.addSubview(label)
views.addSubview(label2)

scrollView.addSubview(views)
self.view!.addSubview(scrollView)
scrollView.contentSize = CGSizeMake(310, 390 * floatValue)
}

所有标签都显示相同的文本。

最佳答案

您在这些行上硬编码了标签文本:

label.text = "iOS LABEL TRY"
label2.text = "iOS LABEL TRY2"

我不确定每个标签中您想要什么文本,但我认为您想将其更改为:

label.text = messageForLbl[j]

此外,您还可以通过使用 Swift 的枚举器来避免 future 的错误(这样您就不必在更改 messageForLbl 时更改 viewLblcount):

for value in messageForLbl.enumerate() {
label.text = value
}

关于ios - 动态创建 UILabel 并同时更改文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37338645/

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