使用Uilabel和UiimageView的编程设置UITATIONVIEWCELL的约束

发布于 2025-02-07 08:17:08 字数 3164 浏览 1 评论 0 原文

我正在尝试使用自定义的uitableviewcell构建我的桌面视图,但面临着一些共同创造的问题。以下是我的自定义UitablViewCell类:

class MovieCell: UITableViewCell {
static let identifier = "MovieCell"
let cellMargin = 15.0

private lazy var containerView: UIView = {
   let view = UIView()
    view.backgroundColor = .lightGray
    view.layer.cornerRadius = 5
    view.translatesAutoresizingMaskIntoConstraints = false
    view.clipsToBounds = true
    return view
}()

private let movieNameLabel: UILabel = {
   let label = UILabel()
    label.translatesAutoresizingMaskIntoConstraints = false
    label.font = UIFont.movieAppBoldFont(size: 15)
    label.numberOfLines = 0
    return label
}()

private let movieImageView: UIImageView = {
    let imageView = UIImageView()
    imageView.translatesAutoresizingMaskIntoConstraints = false
    imageView.contentMode = .scaleAspectFill
    imageView.clipsToBounds = true
    return imageView
}()

var movieCellViewModel : MoviewCellViewModel = MoviewCellViewModel(image: nil, name: "") {
    didSet {
        movieNameLabel.text = movieCellViewModel.name
    }
}

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0)
    layoutSubView()
}

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

private func layoutSubView() {
    addSubview(containerView)
    containerView.addSubview(movieNameLabel)
    containerView.addSubview(movieImageView)
    
    let marginGuide = containerView.layoutMarginsGuide
    
    NSLayoutConstraint.activate([
        containerView.topAnchor.constraint(equalTo: contentView.layoutMarginsGuide.topAnchor),
        containerView.leadingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.leadingAnchor),
        containerView.trailingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.trailingAnchor),
        containerView.bottomAnchor.constraint(equalTo: contentView.layoutMarginsGuide.bottomAnchor, constant: -cellMargin),
        
        movieImageView.leadingAnchor.constraint(equalTo: marginGuide.leadingAnchor, constant: 5),
        movieImageView.topAnchor.constraint(equalTo: marginGuide.topAnchor, constant: 5),
        movieImageView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -5),
        movieImageView.widthAnchor.constraint(equalToConstant: 50),
        movieImageView.heightAnchor.constraint(equalToConstant: 50),

        movieNameLabel.leadingAnchor.constraint(equalTo: marginGuide.trailingAnchor, constant: 5),
        movieNameLabel.trailingAnchor.constraint(equalTo: marginGuide.trailingAnchor, constant: -5),
        movieNameLabel.topAnchor.constraint(equalTo: marginGuide.topAnchor, constant: 5),
        movieNameLabel.bottomAnchor.constraint(equalTo: marginGuide.bottomAnchor, constant: -5)
    ])
}

}

我在上面的代码上获得以下结果:

我正在尝试创建一个Uiview并向其添加内容。我之所以添加它,是因为我想在tableview中显示一些空白空间。

I am trying to build my tableview with custom UITableViewCell but facing some issues in coinstraints. Following is my custom UITablviewCell class:

class MovieCell: UITableViewCell {
static let identifier = "MovieCell"
let cellMargin = 15.0

private lazy var containerView: UIView = {
   let view = UIView()
    view.backgroundColor = .lightGray
    view.layer.cornerRadius = 5
    view.translatesAutoresizingMaskIntoConstraints = false
    view.clipsToBounds = true
    return view
}()

private let movieNameLabel: UILabel = {
   let label = UILabel()
    label.translatesAutoresizingMaskIntoConstraints = false
    label.font = UIFont.movieAppBoldFont(size: 15)
    label.numberOfLines = 0
    return label
}()

private let movieImageView: UIImageView = {
    let imageView = UIImageView()
    imageView.translatesAutoresizingMaskIntoConstraints = false
    imageView.contentMode = .scaleAspectFill
    imageView.clipsToBounds = true
    return imageView
}()

var movieCellViewModel : MoviewCellViewModel = MoviewCellViewModel(image: nil, name: "") {
    didSet {
        movieNameLabel.text = movieCellViewModel.name
    }
}

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0)
    layoutSubView()
}

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

private func layoutSubView() {
    addSubview(containerView)
    containerView.addSubview(movieNameLabel)
    containerView.addSubview(movieImageView)
    
    let marginGuide = containerView.layoutMarginsGuide
    
    NSLayoutConstraint.activate([
        containerView.topAnchor.constraint(equalTo: contentView.layoutMarginsGuide.topAnchor),
        containerView.leadingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.leadingAnchor),
        containerView.trailingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.trailingAnchor),
        containerView.bottomAnchor.constraint(equalTo: contentView.layoutMarginsGuide.bottomAnchor, constant: -cellMargin),
        
        movieImageView.leadingAnchor.constraint(equalTo: marginGuide.leadingAnchor, constant: 5),
        movieImageView.topAnchor.constraint(equalTo: marginGuide.topAnchor, constant: 5),
        movieImageView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -5),
        movieImageView.widthAnchor.constraint(equalToConstant: 50),
        movieImageView.heightAnchor.constraint(equalToConstant: 50),

        movieNameLabel.leadingAnchor.constraint(equalTo: marginGuide.trailingAnchor, constant: 5),
        movieNameLabel.trailingAnchor.constraint(equalTo: marginGuide.trailingAnchor, constant: -5),
        movieNameLabel.topAnchor.constraint(equalTo: marginGuide.topAnchor, constant: 5),
        movieNameLabel.bottomAnchor.constraint(equalTo: marginGuide.bottomAnchor, constant: -5)
    ])
}

}

I get the following result with the above code:
enter image description here

I am trying to create a UIView and add content to it. I am adding it because I want to show some empty space as a separator in tableview.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长梦不多时 2025-02-14 08:17:08

夫妇问题...

您在 layoutsubview()中的第一行是:

addSubview(containerView)

在哪里需要:

contentView.addSubview(containerView)

第二,您有 MoviemageView 被限制在 content> content> contentview

movieImageView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -5),

应将其限制在 Marginguide

movieImageView.bottomAnchor.constraint(equalTo: marginGuide.bottomAnchor, constant: -5),

第三,您具有 Movienamelabel.Leadinginganchor 被限制为 marginguide.trailinginganchor

movieNameLabel.leadingAnchor.constraint(equalTo: marginGuide.trailingAnchor, constant: 5),

在哪里应约束至 MovieimageView.trailinganchor

movieNameLabel.leadingAnchor.constraint(equalTo: movieImageView.trailingAnchor, constant: 5),

进行这些更改将为您提供此(我将图像视图背景设置为蓝色,并标记为青色的标签):

“在此处输入图像说明”

但是,在运行应用程序时,您'请参见很多 无法同时满足约束。 消息。当使用带有单元格中子视图的子视图时,这很常见。

为了摆脱自动投诉,我们可以给出 containerview 底部锚点的优先级较少:

let bottomC = containerView.bottomAnchor.constraint(equalTo: contentView.layoutMarginsGuide.bottomAnchor, constant: -cellMargin)
bottomC.priority = .required - 1

然后在您的约束块中激活该锚:

//containerView.bottomAnchor.constraint(equalTo: contentView.layoutMarginsGuide.bottomAnchor, constant: -cellMargin),
bottomC,

这是您的完整 moviecell 类:

class MovieCell: UITableViewCell {
    static let identifier = "MovieCell"
    let cellMargin = 15.0
    
    private lazy var containerView: UIView = {
        let view = UIView()
        view.backgroundColor = .lightGray
        view.layer.cornerRadius = 5
        view.translatesAutoresizingMaskIntoConstraints = false
        view.clipsToBounds = true
        return view
    }()
    
    private let movieNameLabel: UILabel = {
        let label = UILabel()
        label.translatesAutoresizingMaskIntoConstraints = false
        label.font = .systemFont(ofSize: 15, weight: .bold) // UIFont.movieAppBoldFont(size: 15)
        label.numberOfLines = 0
        return label
    }()
    
    private let movieImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.contentMode = .scaleAspectFill
        imageView.clipsToBounds = true
        return imageView
    }()
    
    var movieCellViewModel : MoviewCellViewModel = MoviewCellViewModel(image: nil, name: "") {
        didSet {
            movieNameLabel.text = movieCellViewModel.name
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0)
        layoutSubView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func layoutSubView() {
        contentView.addSubview(containerView)
        containerView.addSubview(movieNameLabel)
        containerView.addSubview(movieImageView)
        
        let marginGuide = containerView.layoutMarginsGuide
    
        let bottomC = containerView.bottomAnchor.constraint(equalTo: contentView.layoutMarginsGuide.bottomAnchor, constant: -cellMargin)
        bottomC.priority = .required - 1

        NSLayoutConstraint.activate([
            containerView.topAnchor.constraint(equalTo: contentView.layoutMarginsGuide.topAnchor),
            containerView.leadingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.leadingAnchor),
            containerView.trailingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.trailingAnchor),
//          containerView.bottomAnchor.constraint(equalTo: contentView.layoutMarginsGuide.bottomAnchor, constant: -cellMargin),
            bottomC,
            
            movieImageView.leadingAnchor.constraint(equalTo: marginGuide.leadingAnchor, constant: 5),
            movieImageView.topAnchor.constraint(equalTo: marginGuide.topAnchor, constant: 5),
//          movieImageView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -5),
            movieImageView.bottomAnchor.constraint(equalTo: marginGuide.bottomAnchor, constant: -5),
            movieImageView.widthAnchor.constraint(equalToConstant: 50),
            movieImageView.heightAnchor.constraint(equalToConstant: 50),
            
//          movieNameLabel.leadingAnchor.constraint(equalTo: marginGuide.trailingAnchor, constant: 5),
            movieNameLabel.leadingAnchor.constraint(equalTo: movieImageView.trailingAnchor, constant: 5),
            movieNameLabel.trailingAnchor.constraint(equalTo: marginGuide.trailingAnchor, constant: -5),
            movieNameLabel.topAnchor.constraint(equalTo: marginGuide.topAnchor, constant: 5),
            movieNameLabel.bottomAnchor.constraint(equalTo: marginGuide.bottomAnchor, constant: -5)
        ])

        // during dev, so we can easily see the frames
        movieImageView.backgroundColor = .systemBlue
        movieNameLabel.backgroundColor = .cyan
    }
}

Couple issues...

Your first line in layoutSubView() is:

addSubview(containerView)

where is needs to be:

contentView.addSubview(containerView)

Second, you have movieImageView constrained to contentView:

movieImageView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -5),

where it should be constrained to marginGuide:

movieImageView.bottomAnchor.constraint(equalTo: marginGuide.bottomAnchor, constant: -5),

Third, you have movieNameLabel.leadingAnchor constrained to marginGuide.trailingAnchor:

movieNameLabel.leadingAnchor.constraint(equalTo: marginGuide.trailingAnchor, constant: 5),

where it should be constrained to movieImageView.trailingAnchor:

movieNameLabel.leadingAnchor.constraint(equalTo: movieImageView.trailingAnchor, constant: 5),

Making those changes will give you this (I set image view background to blue, and label background to cyan):

enter image description here

However, when you run the app, you'll see lots of Unable to simultaneously satisfy constraints. messages. This is common when using subviews with subviews in cells.

To get rid of the auto-layout complaints, we can give the containerView bottom anchor a less-than-required priority:

let bottomC = containerView.bottomAnchor.constraint(equalTo: contentView.layoutMarginsGuide.bottomAnchor, constant: -cellMargin)
bottomC.priority = .required - 1

and then activate that in your constraints block:

//containerView.bottomAnchor.constraint(equalTo: contentView.layoutMarginsGuide.bottomAnchor, constant: -cellMargin),
bottomC,

Here's your complete MovieCell class with those changes:

class MovieCell: UITableViewCell {
    static let identifier = "MovieCell"
    let cellMargin = 15.0
    
    private lazy var containerView: UIView = {
        let view = UIView()
        view.backgroundColor = .lightGray
        view.layer.cornerRadius = 5
        view.translatesAutoresizingMaskIntoConstraints = false
        view.clipsToBounds = true
        return view
    }()
    
    private let movieNameLabel: UILabel = {
        let label = UILabel()
        label.translatesAutoresizingMaskIntoConstraints = false
        label.font = .systemFont(ofSize: 15, weight: .bold) // UIFont.movieAppBoldFont(size: 15)
        label.numberOfLines = 0
        return label
    }()
    
    private let movieImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.contentMode = .scaleAspectFill
        imageView.clipsToBounds = true
        return imageView
    }()
    
    var movieCellViewModel : MoviewCellViewModel = MoviewCellViewModel(image: nil, name: "") {
        didSet {
            movieNameLabel.text = movieCellViewModel.name
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0)
        layoutSubView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func layoutSubView() {
        contentView.addSubview(containerView)
        containerView.addSubview(movieNameLabel)
        containerView.addSubview(movieImageView)
        
        let marginGuide = containerView.layoutMarginsGuide
    
        let bottomC = containerView.bottomAnchor.constraint(equalTo: contentView.layoutMarginsGuide.bottomAnchor, constant: -cellMargin)
        bottomC.priority = .required - 1

        NSLayoutConstraint.activate([
            containerView.topAnchor.constraint(equalTo: contentView.layoutMarginsGuide.topAnchor),
            containerView.leadingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.leadingAnchor),
            containerView.trailingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.trailingAnchor),
//          containerView.bottomAnchor.constraint(equalTo: contentView.layoutMarginsGuide.bottomAnchor, constant: -cellMargin),
            bottomC,
            
            movieImageView.leadingAnchor.constraint(equalTo: marginGuide.leadingAnchor, constant: 5),
            movieImageView.topAnchor.constraint(equalTo: marginGuide.topAnchor, constant: 5),
//          movieImageView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -5),
            movieImageView.bottomAnchor.constraint(equalTo: marginGuide.bottomAnchor, constant: -5),
            movieImageView.widthAnchor.constraint(equalToConstant: 50),
            movieImageView.heightAnchor.constraint(equalToConstant: 50),
            
//          movieNameLabel.leadingAnchor.constraint(equalTo: marginGuide.trailingAnchor, constant: 5),
            movieNameLabel.leadingAnchor.constraint(equalTo: movieImageView.trailingAnchor, constant: 5),
            movieNameLabel.trailingAnchor.constraint(equalTo: marginGuide.trailingAnchor, constant: -5),
            movieNameLabel.topAnchor.constraint(equalTo: marginGuide.topAnchor, constant: 5),
            movieNameLabel.bottomAnchor.constraint(equalTo: marginGuide.bottomAnchor, constant: -5)
        ])

        // during dev, so we can easily see the frames
        movieImageView.backgroundColor = .systemBlue
        movieNameLabel.backgroundColor = .cyan
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文