UIView в пользовательском UITableViewCell не отображается - PullRequest
0 голосов
/ 19 декабря 2018

Я пытаюсь создать что-то вроде фида в Facebook с UITableView.Тем не менее, у меня есть пользовательский UITableViewCell с некоторыми строками, и я хочу добавить еще один UIView, где я могу добавить Videoplayer.Но почему-то я не вижу дополнительного UIView.

Это то, что я вижу в данный момент:

enter image description here

Вот мой кодмоего пользовательского 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

Пожалуйста, скажите мне, если вам нужны другие фрагменты кода.

1 Ответ

0 голосов
/ 19 декабря 2018

Вы делаете это правильно для других ограничений, но для нижних ограничений uriLabel и otherTestView вы просто забыли сделать их active :

uriLabel.bottomAnchor.constraint(equalTo: otherTestView.bottomAnchor).isActive = true
otherTestView.bottomAnchor.constraint(equalTo: testView.bottomAnchor).isActive = true

Также проблема в том, что вы устанавливаете ограничения otherTestView и testView равными якорям суперпредставления.Это причина того, что otherTestView (добавлено позже) перекрывается testView

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...