UITableView使用约束无法正确显示

时间:2017-11-18 04:12:23

标签: ios swift

我在UITableView下显示此UIImageView时遇到问题。它做了这个奇怪的事情,你无法看到桌子或任何东西,但是当你向上或向下滚动时,你可以看到蓝色单元格出现在40px高UITableView

class TvDetailHeader: UICollectionViewCell, UITableViewDelegate, UITableViewDataSource  {

    private let contributorCellId = "contributorCellId"

    let thumbnailImageView: UIImageView = {
        let imageView = UIImageView(image: #imageLiteral(resourceName: "stranger things poster"))
        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.layer.cornerRadius = 6
        imageView.contentMode = .scaleAspectFill
        imageView.layer.masksToBounds = true
        return imageView
    }()

    let contributorTableView: UITableView = {
        let tableView = UITableView()
        tableView.separatorStyle = .none
        tableView.translatesAutoresizingMaskIntoConstraints = false
        tableView.backgroundColor = .blue
        return tableView
    }()

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: contributorCellId, for: indexPath) as! ContributorCell
        return cell
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 40
    }

    override func setupViews() {
        super.setupViews()

        contributorTableView.delegate = self
        contributorTableView.dataSource = self

        contributorTableView.register(ContributorCell.self, forCellReuseIdentifier: contributorCellId)

        backgroundColor = .white

        addSubview(thumbnailImageView)
        addSubview(contributorTableView)

        thumbnailImageView.topAnchor.constraint(equalTo: topAnchor, constant: 25).isActive = true
        thumbnailImageView.leftAnchor.constraint(equalTo: leftAnchor, constant: 20).isActive = true
        thumbnailImageView.widthAnchor.constraint(equalToConstant: 60).isActive = true
        thumbnailImageView.heightAnchor.constraint(equalToConstant: 90).isActive = true

        contributorTableView.topAnchor.constraint(equalTo: thumbnailImageView.bottomAnchor, constant: 10).isActive = true
        contributorTableView.leftAnchor.constraint(equalTo: leftAnchor, constant: 20).isActive = true
        contributorTableView.widthAnchor.constraint(equalToConstant: frame.width).isActive = true
        contributorTableView.heightAnchor.constraint(equalToConstant: 40).isActive = true
    }
}

class ContributorCell: UITableViewCell  {

    let cellView: UIView = {
        let view = UIView()
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }()

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }

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

    func setupViews() {
        addSubview(cellView)

        backgroundColor = .blue

        cellView.topAnchor.constraint(equalTo: topAnchor, constant: 0).isActive = true
        cellView.leftAnchor.constraint(equalTo: leftAnchor, constant: 0).isActive = true
        cellView.widthAnchor.constraint(equalToConstant: 30).isActive = true
        cellView.heightAnchor.constraint(equalToConstant: frame.height).isActive = true
    }
}

0 个答案:

没有答案
相关问题