Table View Cell 에 VC 넣기 ~!!

Zion·2021년 3월 15일
1

음음 TableView cell에 VC(View Controller)를 넣어야 한다 !

1차 시도

TableCell.swift

        let vc = InfoSlideViewController() // <- 이게 넣고 싶은 VC
        self.contentView.addSubview(vc.view)

결과는? 실패! 보여지긴 보여지지만 VC안에 넣어놨던 버튼이 클릭이 안된다.

2차시도

TableCell.swift

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let controller: UIViewController = storyboard.instantiateViewController(withIdentifier: "InfoSlideViewController") as InfoSlideViewController
        
        self.addChild //이게 안된다

        self.contentView.addSubview(controller.view)

       

아놔,,, UITableCell에는 addChild가 없나보다...
실패.

3차 시도

tableView에 cell 등록해주는건 생략하겠다.

찐 tableView가 있는 TableView Delegate, DataSource부분

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {



            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            pageCell.controller = storyboard.instantiateViewController(withIdentifier: "InfoSlideViewController") as! InfoSlideViewController
            
            self.addChild(pageCell.controller)
            
            pageCell.contentView.addSubview(pageCell.controller.view)
            pageCell.controller.didMove(toParent: self)
            return pageCell
            }

성공... 휴 ;
궁금한게 있다.
[ ]2차시도에서 tableViewCell.swift에선 addChild가 안됐다. 왤까?

참고자료 :
https://stackoverflow.com/questions/21345629/ios-container-view-in-uitableviewcell

profile
어제보다만 나아지는

0개의 댓글