Dynamic Custom UITableView

Zion·2021년 4월 29일
1
post-thumbnail

음음 ! 오늘은 말이지. ViewController 안에 다양한 CustomView들을 넣을거란 말이지 CustomUITableView를 만들어서 ViewController안에 넣어주려고 했는데

view.addSubview(customTableView)

이게 안먹힌다.

그렇담. UIView안에 tableView를 넣어서 써주자.(이제부터 CTable이라 하겠다.)

자. 내가 하고싶은건 말이지 이 CTable의 height를 내가 임의로. 즉 상수로 할당하지 않겠다. 알아서 계산해서 깔자 이말이야. 그렇담 이 CTable의 height를 알아야 한다.
항상 부딪히는 Dynamic크기 문제이다 ㅎㅎ 한두번도 아니고 ! 해봅시다.

protocol heightDelegate {
    func fh(h: CGFloat)
}
class CTableView: CustomView {
	override func layoutSubviews() {
        super.layoutSubviews()
        table.layoutIfNeeded()       
        print("layoutIfNeed")
        let frame: CGRect = table.frame
        table.frame = CGRect(x: 0.0, y: 0.0, width: frame.width, height: table.contentSize.height) //table.contentSize.height

        delegate?.fh(h: table.contentSize.height)
        
    }
    

}

extension CTableView: UITableViewDelegate, UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return "헤드 입니다."
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! UITableViewCell
        cell.textLabel!.text = "됐음 좋겠다"
        cell.backgroundColor = #colorLiteral(red: 0.721568644, green: 0.8862745166, blue: 0.5921568871, alpha: 1)
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 30
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        self.layoutSubviews()
        print("called willDisplay natural")
    }
}


기본크기가 248에서 시작해서 계속 update된다
이상 쓸 ViewController에 update시켜주면 된다.

profile
어제보다만 나아지는

0개의 댓글