[英]"Generate" labels from data swift
I have我有
["06:58"]
["07:13", "07:38", "07:53"]
["08:13", "08:38", "08:53"]
["09:13", "09:33", "09:53"]
and I want to show it in tableView
as in picture:我想在
tableView
中显示它,如图所示:
every element - it's UILabel
, I created class TimeViewCell: UITableViewCell
where have property var labels = [UILabel]()
but I don't know how to push data from my array in my function:每个元素 - 它是
UILabel
,我创建了class TimeViewCell: UITableViewCell
其中有属性var labels = [UILabel]()
但我不知道如何在我的函数中从我的数组中推送数据:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellTime", for: indexPath) as! TimeViewCell
let showHour = self.infoWripper.sorted(by: { $0.hour < $1.hour })
cell.labelTime.text = showHour[indexPath.row].showHour()
for data in showHour {
print(data.showFullTime()) //here you see my example of data in console
}
return cell
}
I would add a UIStackView to your UITableViewCell.我会向您的 UITableViewCell 添加一个 UIStackView。 You can add the labels with the text to the UIStackView.
您可以将带有文本的标签添加到 UIStackView。
I wrapped your data into an array.我将您的数据包装到一个数组中。
let tableViewData = [["06:58"],
["07:13", "07:38", "07:53"],
["08:13", "08:38", "08:53"],
["09:13", "09:33", "09:53"]]
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellTime", for: indexPath) as! TimeViewCell
cell.configure(data: tableViewData[indexPath.row])
return cell
}
Then, in for your TimeViewCell, you have to add your UIStackView IBOulet (or add it programmatically) and the configure method.然后,在您的 TimeViewCell 中,您必须添加您的 UIStackView IBoulet(或以编程方式添加)和配置方法。
class TimeViewCell: UITableViewCell {
@IBOutlet var labelStackView: UIStackView!
func configure(data: Array<String>) {
for time in data {
let timeLabel = UILabel()
timeLabel.text = time
labelStackView.addArrangedSubview(label)
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.