Swift Playground UITable View Cell在添加子视图后未调整大小

8iwquhpp  于 2023-05-16  发布在  Swift
关注(0)|答案(1)|浏览(110)

the table view loads
the sub view loads but you can see it is over other cells
sometimes the sub view loads but other cells are on top of it
我有一个UITableView,当用户触摸一行时,一个子视图被添加到屏幕上。当他们单击另一行时,最后一个子视图将消失(该行应收缩回其正常大小)。子视图显示得很好(单击另一行时它会消失),但它会挂在行外并覆盖其他一些选项。此外,当屏幕旋转后重新加载表视图时,子视图将位于表的其他行后面。
我希望调整包含子视图的行的大小,使其与子视图的框架匹配。此外,每个子视图的大小都不同,具体取决于其中包含的文本和图像。正因为如此,我尝试使用自动布局和UITableViewAutomaticDimension,但似乎没有什么工作。
我包括我的UITableViewController,这样你就可以看到我有什么,可以引导我在正确的方向。

class TableViewController : UITableViewController {

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    tableView.backgroundColor = #colorLiteral(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0)
    self.tableView.estimatedRowHeight = rowHeight
    return filmsToDisplay.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    var currentFilm = filmsToDisplay[indexPath.row]
    let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
    cell.selectionStyle = .none
    cell.textLabel?.text = "\(currentFilm.year) - \(currentFilm.movieTitle)"
    cell.textLabel?.textColor = #colorLiteral(red: 0.254901975393295, green: 0.274509817361832, blue: 0.301960796117783, alpha: 1.0)
    cell.textLabel?.font = UIFont.systemFont(ofSize: fontSize)
    cell.textLabel?.sizeToFit()
    cell.backgroundColor = #colorLiteral(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0)
    cell.contentView.backgroundColor = #colorLiteral(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0)


    let accessory = makeChart(currentFilm: filmsToDisplay[indexPath.row])
    cell.contentView.addSubview(accessory)
    accessory.translatesAutoresizingMaskIntoConstraints = false
    accessory.trailingAnchor.constraint(equalTo:cell.contentView.trailingAnchor).isActive = true
    accessory.topAnchor.constraint(equalTo: cell.contentView.topAnchor).isActive = true
    accessory.bottomAnchor.constraint(equalTo: cell.contentView.bottomAnchor).isActive = true
    accessory.widthAnchor.constraint(equalTo: cell.contentView.widthAnchor, multiplier: nomBarWidth).isActive = true

    cell.contentView.translatesAutoresizingMaskIntoConstraints=false
    cell.contentView.leadingAnchor.constraint(equalTo:cell.leadingAnchor).isActive = true
    cell.contentView.trailingAnchor.constraint(equalTo:cell.trailingAnchor).isActive = true
    cell.contentView.topAnchor.constraint(equalTo:cell.topAnchor).isActive = true
    cell.contentView.bottomAnchor.constraint(equalTo: cell.bottomAnchor).isActive = true

    return cell
}

var lastSelectedCell : UITableViewCell? = nil
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let currentFilm = filmsToDisplay[indexPath.row]
    var detailView = createView(film: currentFilm)
    detailView.tag = 555
    let selectedCell:UITableViewCell = tableView.cellForRow(at: indexPath)!
    //get rid of last detail view
    if let lastCell = lastSelectedCell {
        for i in 0..<lastCell.contentView.subviews.count {
            let content = lastCell.contentView.subviews[i]
            if content.tag == 555{
                content.removeFromSuperview()
            }
        }
    }
    selectedCell.contentView.addSubview(detailView)
    detailView.translatesAutoresizingMaskIntoConstraints=false
    detailView.leadingAnchor.constraint(equalTo:selectedCell.contentView.leadingAnchor).isActive = true
    detailView.trailingAnchor.constraint(equalTo:selectedCell.contentView.trailingAnchor).isActive = true
    detailView.topAnchor.constraint(equalTo:selectedCell.contentView.topAnchor).isActive = true
    detailView.bottomAnchor.constraint(equalTo:detailView.subviews[detailView.subviews.count - 2].bottomAnchor).isActive = true
    lastSelectedCell = selectedCell
}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

}
子视图的片段定义其高度如下(有更多片段,但它们都具有相同类型的约束:

//MOVIE TITLE BAR
let movieTitleBar = UIButton()
movieTitleBar.setTitleColor(#colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0), for: UIControlState.normal)
movieTitleBar.setTitle(film.movieTitle, for: UIControlState.normal)
    movieTitleBar.backgroundColor = #colorLiteral(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0)
    movieTitleBar.alpha = 0.5
    movieTitleBar.layer.borderWidth = outlineWidth
    movieTitleBar.layer.borderColor = #colorLiteral(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0)
}
filmView.addSubview(movieTitleBar)
movieTitleBar.translatesAutoresizingMaskIntoConstraints = false
movieTitleBar.topAnchor.constraint(equalTo:filmView.topAnchor).isActive = true
movieTitleBar.heightAnchor.constraint(equalToConstant: rowHeight).isActive = true
movieTitleBar.leadingAnchor.constraint(equalTo:filmView.leadingAnchor).isActive = true
movieTitleBar.trailingAnchor.constraint(equalTo:filmView.trailingAnchor).isActive = true

//POSTER IMAGE
var poster = UIButton()
isPoster = false
if let posterImg = film.image {
    if let url = NSURL(string:posterImg){
        if let data = NSData(contentsOf:url as URL){
            film.poster = UIImage(data:data as Data)
            poster.setImage(film.poster, for: UIControlState.normal)
            isPoster = true
            filmView.addSubview(poster)
        }
    }
}

//BRIEF DESCRIPTION LABEL
let briefDescriptionBar = UILabel()
briefDescriptionBar.backgroundColor = film.colorUI
briefDescriptionBar.alpha = 0.8
briefDescriptionBar.text = "\(film.year) - \(film.briefDescription)"
briefDescriptionBar.textColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
briefDescriptionBar.numberOfLines = 0
briefDescriptionBar.textAlignment = .center
filmView.addSubview(briefDescriptionBar)
briefDescriptionBar.translatesAutoresizingMaskIntoConstraints = false
briefDescriptionBar.leadingAnchor.constraint(equalTo:movieTitleBar.leadingAnchor).isActive = true
briefDescriptionBar.topAnchor.constraint(equalTo:movieTitleBar.bottomAnchor).isActive = true
if isPoster == true {
    poster.translatesAutoresizingMaskIntoConstraints = false
    poster.topAnchor.constraint(equalTo:movieTitleBar.bottomAnchor).isActive = true
    poster.trailingAnchor.constraint(equalTo:movieTitleBar.trailingAnchor).isActive = true
    poster.widthAnchor.constraint(equalTo:filmView.widthAnchor, multiplier:nomBarWidth).isActive = true
    let aspect = (nomBarWidth * 41) / 27
    poster.heightAnchor.constraint(equalTo: filmView.widthAnchor, multiplier: aspect).isActive = true
    briefDescriptionBar.trailingAnchor.constraint(equalTo:poster.leadingAnchor).isActive = true
    briefDescriptionBar.bottomAnchor.constraint(equalTo:poster.bottomAnchor).isActive = true
}
else {
    briefDescriptionBar.widthAnchor.constraint(equalTo:filmView.widthAnchor).isActive = true
    briefDescriptionBar.heightAnchor.constraint(equalTo:movieTitleBar.heightAnchor, multiplier : 2).isActive = true
}
63lcw9qa

63lcw9qa1#

你的问题是,你没有给你添加的子视图一个高度约束,你只挂起了leading,trailing,top和bottom,但是contentView期望它的高度来自内部子视图(如果它们没有像标签/按钮这样的内在内容大小),也不需要这部分代码

cell.contentView.translatesAutoresizingMaskIntoConstraints=false
cell.contentView.leadingAnchor.constraint(equalTo:cell.leadingAnchor).isActive = true
cell.contentView.trailingAnchor.constraint(equalTo:cell.trailingAnchor).isActive = true
cell.contentView.topAnchor.constraint(equalTo:cell.topAnchor).isActive = true
cell.contentView.bottomAnchor.constraint(equalTo: cell.bottomAnchor).isActive = true

另外,contentView内部应该只有一个视图连接到底部,如果添加了多个视图,那么如果两个视图都连接到它的顶部,就会产生冲突,因此在连接新视图之前必须打破last view bottom约束

相关问题