Swift 4: установка высоты UITableViewCell для динамического размера в зависимости от раздела tableViews - PullRequest
0 голосов
/ 26 августа 2018

Я пытаюсь добиться правильной методики определения размера для моего 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)

    }
}
...