iOS Uikit-如何以编程方式创建自定义UIVIEW

发布于 2025-01-26 10:21:15 字数 3024 浏览 4 评论 0原文

我正在尝试创建一个自定义uiview(cardView),并在UITAITEVIEW中显示

class CardView: UIView {

    public var cardTitle = UILabel()
    public var cardValue = UILabel()


    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .secondarySystemBackground
    
        self.addSubview(cardTitle)
        self.addSubview(cardValue)
    
        layer.cornerRadius = 10
        
        cardTitle.frame = CGRect(x: 0, y: 10, width: self.width, height: 25)
        cardValue.frame = CGRect(x: 0, y: cardTitle.bottom + 1, width: self.width, height: 25)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

TableView如下:

class StatsTableViewCell: UITableViewCell {

    static let identifier = "StatsID"

    private var cellTitleLabel: UILabel = {
        let label = UILabel()
        return label
    }()

    private var homeView: CardView = {
        let card = CardView()
        card.cardTitle.text = "Home"
        return card
    }()

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        addSubview()
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        cellTitleLabel.frame = CGRect(x: 10, y: 10, width: contentView.width - 20, height: 20)
        homeView.frame = CGRect(x: 10, y: cellTitleLabel.bottom + 10, width: (contentView.width - 40) / 3, height: contentView.height - 25)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func addSubview() {
        contentView.addSubview(cellTitleLabel)
        contentView.addSubview(homeView)
    }

    func configure(with viewModel: StatsTableviewViewModel) {
        cellTitleLabel.text = viewModel.cellTitle
        homeView.cardValue.text = viewModel.homeValue
    }

}

在我的cellforrowat上,我可以尝试像这样配置单元格:

extension WelcomeViewController: UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        viewmodels.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: StatsTableViewCell.identifier, for: indexPath) as? StatsTableViewCell else {
            fatalError()
        }
        print(viewmodels)
        cell.configure(with: viewmodels[indexPath.row])
        return cell
    }
}

问题在于,在statsviewController中,它显示了Homeview背景和celltitlelabel,但未显示cardTitle and Code>和cardValue

statStableView

i 的屏幕截图m使用扩展名更容易访问宽度和高度。这就是为什么您没有看到.frame.size.height

I'm trying to create a custom UIView (CardView) and display it in a UITableView

class CardView: UIView {

    public var cardTitle = UILabel()
    public var cardValue = UILabel()


    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .secondarySystemBackground
    
        self.addSubview(cardTitle)
        self.addSubview(cardValue)
    
        layer.cornerRadius = 10
        
        cardTitle.frame = CGRect(x: 0, y: 10, width: self.width, height: 25)
        cardValue.frame = CGRect(x: 0, y: cardTitle.bottom + 1, width: self.width, height: 25)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

The TableView looks like this:

class StatsTableViewCell: UITableViewCell {

    static let identifier = "StatsID"

    private var cellTitleLabel: UILabel = {
        let label = UILabel()
        return label
    }()

    private var homeView: CardView = {
        let card = CardView()
        card.cardTitle.text = "Home"
        return card
    }()

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        addSubview()
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        cellTitleLabel.frame = CGRect(x: 10, y: 10, width: contentView.width - 20, height: 20)
        homeView.frame = CGRect(x: 10, y: cellTitleLabel.bottom + 10, width: (contentView.width - 40) / 3, height: contentView.height - 25)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func addSubview() {
        contentView.addSubview(cellTitleLabel)
        contentView.addSubview(homeView)
    }

    func configure(with viewModel: StatsTableviewViewModel) {
        cellTitleLabel.text = viewModel.cellTitle
        homeView.cardValue.text = viewModel.homeValue
    }

}

On my cellForRowAt im tryin to configuring the cell like this:

extension WelcomeViewController: UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        viewmodels.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: StatsTableViewCell.identifier, for: indexPath) as? StatsTableViewCell else {
            fatalError()
        }
        print(viewmodels)
        cell.configure(with: viewmodels[indexPath.row])
        return cell
    }
}

The problem is that in the StatsViewController it shows the homeView background and the cellTitleLabel but is not showing the cardTitle and cardValue

Screenshot of the StatsTableView

I'm using an extension to access easier to width and height. That's why you're not seeing .frame.size.height

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文