1

每当我尝试向 StackView 添加图像、gif 或视频时,单元格都不会调整大小以适应媒体。媒体只是与 StackView 内的所有内容重叠。

class HubTableViewCell: UITableViewCell {

@IBOutlet weak var labelContent: UILabel!
@IBOutlet weak var stackViewContainer: UIStackView!

var mediaImage:UIImageView?

override func layoutSubviews() {
    super.layoutSubviews()
    mediaImage = UIImageView(frame: CGRect(x: 0, y: 0, width: stackViewContainer.bounds.width, height: 420))
    mediaImage?.kf_setImageWithURL(NSURL(string: "https://placem.at/people")!)
    mediaImage?.clipsToBounds = true
    mediaImage?.contentMode = .ScaleAspectFill
    mediaImage?.widthAnchor.constraintEqualToConstant(stackViewContainer.bounds.width).active = true
    mediaImage?.heightAnchor.constraintEqualToConstant(420).active = true
    if let mediaImage = mediaImage {
        stackViewContainer.insertSubview(mediaImage, aboveSubview: labelContent)
        stackViewContainer.bringSubviewToFront(mediaImage)
    }
}

override func prepareForReuse() {
    super.prepareForReuse()
    if let mediaImage = mediaImage {
        stackViewContainer.removeArrangedSubview(mediaImage)
        mediaImage.removeFromSuperview()
    }
    mediaImage = nil
}

 }
4

1 回答 1

0
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Identifier") as! UITableViewCell
    cell.yourUIStackView.addArrangedSubview(yourView)

}
于 2016-10-26T18:55:59.387 回答