我正在尝试以编程方式创建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)
}
所有标签都显示相同的文字。
答案 0 :(得分:1)
您在这些行上有硬编码的标签文字:
label.text = "iOS LABEL TRY"
label2.text = "iOS LABEL TRY2"
我不确定每个标签中你想要的文字,但我认为你想把它改成:
label.text = messageForLbl[j]
此外,您可以使用Swift的枚举器来避免未来的错误(当您更改viewLblcount
时,您不必更改messageForLbl
):
for value in messageForLbl.enumerate() {
label.text = value
}
答案 1 :(得分:0)
因为您对文本进行了硬编码
label.text =&#34; iOS LABEL TRY&#34;