gpt4 book ai didi

ios - 如何在 CGRectMake 函数中使用变量 - 尝试以表格结构输出数据

转载 作者:行者123 更新时间:2023-11-28 13:02:24 24 4
gpt4 key购买 nike

我试图通过实用地创建和堆叠标签来输出一个帐户的“ friend ”列表。我已经从 MySQL 数据库(可通过与 PHP 脚本交互的 NSURL 访问)中获取帐户的 friend 并将它们加载到一个数组中。目标是遍历数组并创建一个标签,每次迭代时该标签的位置略低于前一个标签。我遇到的问题是,当我将一个变量传递给 CGRectMake 函数来创建标签时,它不会接受 int,因为它不是“CGFloat”类型。我觉得我几乎尝试了所有方法,但我永远无法正确转换数据类型。
这是我的代码:

import UIKit

class viewFriendsViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
let friendsArray = ["billy", "joe", "bob", "jim"]
var inc = 0
for index in friendsArray {
let label: UILabel = UILabel()
let yPos: CGFloat = Float(inc * 10)
label.frame = CGRectMake(50, 50, 200, yPos)
label.backgroundColor = UIColor.blackColor()
label.textColor = UIColor.whiteColor()
label.textAlignment = NSTextAlignment.Center
label.text = "\(index)"
self.view.addSubview(label)
inc = inc + 1 // I have not been able to play with the label yet to determine how to move it's vertical position because I cannot run the program due to the casting error.
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()

}

}

最佳答案

试试这个

var inc = 0
for index in friendsArray {
let label: UILabel = UILabel()

//type conversion is
let yPos = CGFloat(inc * 10)

label.frame = CGRectMake(50,yPos,200,50) // if you want to change the Y-Coodinates use in Y frame not in height
label.backgroundColor = UIColor.blackColor()
label.textColor = UIColor.whiteColor()
label.textAlignment = NSTextAlignment.Center
label.text = "\(index)"
self.view.addSubview(label)
inc = inc + 1
}

上面的选择不行试试这个

  var inc : CGFloat = 0
for index in friendsArray {
let label: UILabel = UILabel()

label.frame = CGRectMake(50,inc,200,50) // if you want to change the Y-Coodinates use in Y frame not in height
label.backgroundColor = UIColor.blackColor()
label.textColor = UIColor.whiteColor()
label.textAlignment = NSTextAlignment.Center
label.text = "\(index)"
self.view.addSubview(label)
inc = inc * 10 // inc + 10
}

关于ios - 如何在 CGRectMake 函数中使用变量 - 尝试以表格结构输出数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33548347/

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