Swift 4:根据tableViews部分将UITableViewCell的高度设置为动态大小

时间:2018-08-26 18:14:46

标签: swift uitableview tableview dynamic-resizing

我要实现的是根据部分为TableViewCell提供适当的大小调整技术。我的tableView有两个部分。

第一部分硬编码为260的高度,我希望第二部分根据其中的textView的大小动态调整大小。

heightForRowAtIndexPath

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if indexPath.section == 0 {
        return 260
    } else {
//WHY ISN'T THIS AUTO RESIZING?
        tableView.estimatedRowHeight = 500
        return UITableViewAutomaticDimension
    }
}

UITableViewCell

class BlogCell: UITableViewCell {
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    //setUpContainerView()
    backgroundColor = Color.backgroundColor

    setUpBlogDescription()

}

let blogImageView: UIImageView = {
    let imageView = UIImageView()
    imageView.image = UIImage(named: "blankProfile")
    imageView.translatesAutoresizingMaskIntoConstraints = false
    imageView.layer.cornerRadius = 38
    imageView.clipsToBounds = true
    imageView.contentMode = .scaleAspectFill
    return imageView
}()

let blogTitle: UITextView = {
    let textView = UITextView()
    textView.text = "What Does Sustainbility Look Like?"
    textView.font = UIFont(name: "Avenir Next", size: 18.0)
    textView.font = UIFont.boldSystemFont(ofSize: 18)
    textView.isScrollEnabled = false
    textView.backgroundColor = .clear
    textView.textColor = Color.blue
    return textView
}()

func setUpBlogDescription() {
    addSubview(blogImageView)
    addSubview(blogTitle)


    blogImageView.setAnchor(top: topAnchor, left: leftAnchor, bottom: nil, right: nil, paddingTop: 8, paddingLeft: 15, paddingBottom: 0, paddingRight: 0)
    blogImageView.heightAnchor.constraint(equalToConstant: 75).isActive = true
    blogImageView.widthAnchor.constraint(equalToConstant: 75).isActive = true

    blogTitle.setAnchor(top: topAnchor, left: blogImageView.rightAnchor, bottom: nil, right: rightAnchor, paddingTop: 8, paddingLeft: 8, paddingBottom: 0, paddingRight: 5)

    }
}

0 个答案:

没有答案