ios - "Generate"来自 data swift 的标签

标签 ios swift tableview uitableview

我有

["06:58"]
["07:13", "07:38", "07:53"]
["08:13", "08:38", "08:53"]
["09:13", "09:33", "09:53"]

我想在 tableView 中显示它,如图所示:

enter image description here

每个元素 - 它是 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
    }

最佳答案

我会向您的 UITableViewCell 添加一个 UIStackView。您可以将带有文本的标签添加到 UIStackView。

我把你的数据包装成一个数组。

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
}

然后,在您的 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)
        }
    }
}

关于ios - "Generate"来自 data swift 的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41569336/

相关文章:

swift 3 计算到当前位置的距离并将结果从最近到最远排序

ios - 不同 View Controller 中的同名变量

ios - UIButton 设置图像和标题

ios - 使用 MPMoviePlayerController 播放录制的视频时 iOS 7 中的 _itemFailedToPlayToEnd 问题

javascript - 以编程方式在 WebView 中以全屏方式打开 youtube 视频的 iframe - iOS

ios - didReceiveIncomingPushWith 未调用

ios - 在 swift 4 中使用 startMonitoringSignificantLocationChanges 的正确方法是什么?

ios - AWS SDK iOS - 如何确定上传请求的完成百分比

mysql - 我的表格 View 显示空白行

使用数据类填充 TableView 时出现 java.lang.ClassCastException