티스토리 뷰

제목에서 오늘 글의 전부가 표현되어 있는 것 같습니다. Storyboard로 뷰를 구성하다가 CustomView와 TableView를 함께 쓰고 싶을 때가 있습니다. 그럴 땐 View와 TableView를 Object Library에서 끌어다 쓰면 되겠지만 TableView의 content가 Dynamic Prototype이 아닌 Static Cells라면 위 방법을 쓸 수 없습니다.

 

 UITableView에서 Static Cell을 사용하려고 하면 UITableViewController에 embed될 때만 사용 가능하다며 다음과 같은 에러가 뜨죠.

Static table views are only valid when embedded in UITableViewController instances

뭐... UITableViewController를 ViewController에 embed 해주는 방식으로 진행하면 잘 해결이 됩니다! (출처: StackOverflow) 아래의 방법을 따라하면 잘 될거에요.

해결하기

1. Storyboard에 ViewController를 두고 ContainerView를 ViewController내에 둡니다.

2. ContainerView를 만들면 자동으로 생성되는 ViewController와 Segue를 지우고 새로운 UITableViewController를 만들어서 연결한 뒤 'ViewController'로 embed하여 segue 설정을 합니다.

(좌) ViewController 내에 둔 ContainerView (우) UITableViewController

3. UITableViewController의 모양이 바뀌면서 embed 되었음을 확인합니다.

왜?

Static cell과 Dynamic prototype의 차이는 Storyboard에서 바로 작성이 가능한가 아닌가입니다.

Static cell을 사용하게 되면 table view에 들어가야 할 내용(datasource)들이 IB를 통해 제공 되는 것이고

dynamic prototype은 내용들을 UITableViewDatasource의 메소드들을 통해 제공합니다.

// Return the number of rows for the table.     
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
   return 0
}

// Provide a cell object for each row.
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   // Fetch a cell of the appropriate type.
   let cell = tableView.dequeueReusableCell(withIdentifier: "cellTypeIdentifier", for: indexPath)
   
   // Configure the cell’s contents.
   cell.textLabel!.text = "Cell text"
       
   return cell
}

출처: AppleDocument - UITableViewDataSource

Dynamic Prototype을 통해 table view를 구성할 때는 위의 코드를 통해 알려줬기 때문에 UITableView를 써도 별 문제가 되지 않지만 

Static Cell들의 data source는 UITableViewController가 제공하는 특별한 방법을 통해 관리 됩니다. (UITableViewController provides some extra magic to take care of the data source for the static cells.) 그렇기 때문에 UITableViewController에 embed 하라는 에러 메세지를 마주하게 되는 것이죠.

(출처 :  Raywenderlich - Beginning Storyboards in iOS 5 Part 2)

 

 

donaricano-btn

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함