简体   繁体   English

从数据快速“生成”标签

[英]"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.

相关问题 如何在Swift中将数据从数组分解为表中的标签? - How to decompose data from an array into labels in a table in Swift? 为什么我不能在Swift中使用从一个UIViewController传递到另一个UIViewController的数据来设置标签(在控制台中打印数据)? - Why I cannot set the labels with the data passed from one UIViewController to another (printing the data in console works) in Swift? 想要将API数据显示到标签(Swift,Alamofire) - Want to display API data to labels (Swift, Alamofire) 领域数据已保存到数据库,但未在Swift 4中填充标签 - Realm data is saved to database but is not populating labels in Swift 4 Swift:将UserDefaults词典数据检索到标签中 - Swift: Retrieving UserDefaults dictionary data into labels 快速比较不同行中的两个标签 - Compare two labels from different rows in swift 从 Swift 在 iOS 上生成 UUID - Generate a UUID on iOS from Swift Swift:由于按下按钮,如何将来自三个单独文本字段的数据发布到自定义单元格的标签上? - Swift: How can I post data from three separate text fields onto labels in a custom cell, as the result of pressing a button? 使用 Swift 5.0 隐藏 HIChart 饼图的数据标签 - Hide data labels of a HIChart's Pie chart using Swift 5.0 如何从数组中生成swift中的字典 - How to generate a dictionary in swift from an array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM