...

반응형

Tableview뷰를 사용하다 보면 Tableview cell의 height를 고정이 아닌 동적으로 크기를 조절해야 될때가 있습니다

 

일단 viewDidLoad에 아래를 추가 한 다음

 

tableview.estimatedRowHeight = UITableView.automaticDimension
tableview.rowHeight = UITableView.automaticDimension

 

아래 함수를 추가해 주면 됩니다

 

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableView.automaticDimension
}
    
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableView.automaticDimension
}

 

만약 최소 높이를 고정하고 싶다면 스토리 보드에서 height constraint를 >= 조건으로 만들면 최소 조건 크기도 설정할 수 있습니다

반응형