Ios 自定义UITableViewCell中的UIView不可见

Ios 自定义UITableViewCell中的UIView不可见,ios,swift,uitableview,Ios,Swift,Uitableview,我正在尝试构建一个类似于facebook的订阅源,并带有UITableView。但是,我有一个带有一些字符串的自定义UITableViewCell,我想添加另一个UIView,在那里我可以添加视频播放器。但不知何故,我看不到额外的UIView 这就是我现在看到的: 以下是我的自定义TableViewCell的代码: import Foundation import UIKit class LandingPostTableViewCell: UITableViewCell { var post

我正在尝试构建一个类似于facebook的订阅源,并带有UITableView。但是,我有一个带有一些字符串的自定义UITableViewCell,我想添加另一个UIView,在那里我可以添加视频播放器。但不知何故,我看不到额外的UIView

这就是我现在看到的:

以下是我的自定义TableViewCell的代码:

import Foundation
import UIKit

class LandingPostTableViewCell: UITableViewCell {
var post : Post?

lazy var nameLabel = createUITextView()
lazy var uriLabel = createUITextView()
lazy var dateLabel = createUITextView()

var testView : UIView = {
    //let testView = UIView.init()
    let testView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
    testView.translatesAutoresizingMaskIntoConstraints = false
    testView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    testView.contentMode = .scaleAspectFit
    testView.backgroundColor = .blue
    testView.clipsToBounds = true
    return testView
}()

var otherTestView : UIView = {
    //let otherTestView = UIView.init()
    let otherTestView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 200))
    otherTestView.translatesAutoresizingMaskIntoConstraints = false
    otherTestView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    otherTestView.contentMode = .scaleAspectFit
    otherTestView.backgroundColor = UIColor(red: 100.0/255.0, green: 130.0/255.0, blue: 230.0/255.0, alpha: 1.0)
    otherTestView.clipsToBounds = true
    return otherTestView
}()

override func awakeFromNib() {
    super.awakeFromNib()
}

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

    self.setupViews()
}

override func layoutSubviews() {
    super.layoutSubviews()

    if let post = post {
        nameLabel.text = "\(post.author.firstName!) \(post.author.lastName!)"
        uriLabel.text = post.video.uri
        dateLabel.text = post.createdAt
    }

}

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

func setupViews() -> () {
    self.contentView.addSubview(testView)
    self.contentView.addSubview(otherTestView)

    testView.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor).isActive = true
    testView.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor).isActive = true
    testView.topAnchor.constraint(equalTo: self.contentView.topAnchor).isActive = true
    testView.bottomAnchor.constraint(equalTo: otherTestView.topAnchor).isActive = true

    otherTestView.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor).isActive = true
    otherTestView.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor).isActive = true
    otherTestView.topAnchor.constraint(equalTo: testView.bottomAnchor).isActive = true
    otherTestView.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor).isActive = true
}

func createUITextView() -> UILabel {
    let textView = UILabel()
    textView.translatesAutoresizingMaskIntoConstraints = false

    return textView
}
}
我不明白,为什么我看不到有黑色背景和我的uriLabel的otherTestView

我的tableview行高设置为automaticDimension:

self.postsTableView.rowHeight = UITableView.automaticDimension

请告诉我您是否需要任何其他代码段。

对于其他约束,您做得不错,但是对于uriLabel和其他TestView的底部约束,您只是忘记了将它们激活:


另外,问题是您正在将其他testView和testView的约束设置为与superview的锚相同。这导致后面添加的otherTestView与testView重叠

感谢您的提示!我已经更新了我的TableViewCell,并简化了我的示例。但是,现在我只看到了otherTestView,而没有看到testView:/有什么想法吗?@FuchurKool这是因为您将两个视图的约束设置为superview的约束,所以otherTestView与我看到的testView重叠,所以您能给我举个例子说明如何正确执行此操作吗?我想如果我把俯视图的下锚放到俯视图的上锚,它应该在它上面吗?@FuchurKool听着,想象一下,就像你在另一个上面给纸一样。你看不到第一张纸在第二张纸下面。你要做的是把文件放在一起。那你就可以看到这两份文件了。我不知道怎么才能做到?我应该在第二个视图上使用insertSubview方法吗?
uriLabel.bottomAnchor.constraint(equalTo: otherTestView.bottomAnchor).isActive = true
otherTestView.bottomAnchor.constraint(equalTo: testView.bottomAnchor).isActive = true