본문 바로가기

Project/MovieClip

테이블의 rowheight을 동적으로 할 때 주의할 점

1️⃣ tableView.estimatedRowHeight 설정이 누락됨

UITableView.automaticDimension을 사용하려면 estimatedRowHeight를 설정
해결 방법: viewDidLoad()에서 estimatedRowHeight 값을 추가

 

override func viewDidLoad() {
    super.viewDidLoad()
    
    detailTableView.delegate = self
    detailTableView.dataSource = self

    // ✅ 높이 자동 조절 설정
    detailTableView.estimatedRowHeight = 100  // 임의의 예상 높이 설정
    detailTableView.rowHeight = UITableView.automaticDimension
}

 

2️⃣ UILabel의 translatesAutoresizingMaskIntoConstraints = false 누락 여부

이미 코드에는 설정되어 있지만, 혹시 contentView의 크기가 자동으로 조절되지 않는 경우를 대비하여 오토레이아웃을 명확히 보장해야 합니다.

해결 방법:

  1. contentView.bottomAnchor가 overviewLabel.bottomAnchor와 정확히 연결되었는지 확인
  2. compressionResistancePriority와 huggingPriority를 추가하여 UILabel이 자연스럽게 크기를 변경하도록 설정
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    contentView.backgroundColor = .black

    // ✅ 셀 자체의 크기를 유동적으로 변경
    contentView.translatesAutoresizingMaskIntoConstraints = false

    // ✅ UILabel이 자동 크기 조절을 우선하도록 설정
    overviewLabel.setContentHuggingPriority(.defaultHigh, for: .vertical)
    overviewLabel.setContentCompressionResistancePriority(.defaultHigh, for: .vertical)

    configureConstraints()
}