我正在尝试使用UITableView构建类似Facebook feed的东西。但是,我有一个带有一些字符串的自定义UITableViewCell,我想添加另一个UIView,可以在其中添加一个Videoplayer。但是以某种方式,我看不到其他的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
}
}
我不明白,为什么看不到黑色背景的“ otherTestView”和内部的“ uriLabel”?
我的表格视图行高设置为automaticDimension:
self.postsTableView.rowHeight = UITableView.automaticDimension
请告诉我是否需要其他代码段。
答案 0 :(得分:0)
对于其他约束,您这样做是正确的,但是对于uriLabel
和otherTestView
的底部约束,您只是忘记使其变为 active :
uriLabel.bottomAnchor.constraint(equalTo: otherTestView.bottomAnchor).isActive = true
otherTestView.bottomAnchor.constraint(equalTo: testView.bottomAnchor).isActive = true
问题还在于,您要设置otherTestView
和testView
的约束等于Superview的锚点。这导致otherTestView
(稍后添加)与testView