TableView๋ฅผ ์ฌ์ฉํ๋ฉด์ Cell์ ์ฌ์ฌ์ฉํ๊ธฐ ์ํด extension์ผ๋ก UITableViewDataSource๋ฅผ ์ฌ์ ์ํ๊ณ ๋น๋๋ฅผ ์งํํ๋ค.
extension ViewController : UITableViewDataSource {
func tableView(_ tableView : UITableView, numberOfRowsInSection section : Int) -> Int {
return self.tasks.count
}
func tableView(_ tableView : UITableView, cellForRowAt indexPath : IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for : indexPath)
let task = self.tasks[indexPath.row]
cell.textLabel?.text = task.title
return cell
}
}
์ฑ์ ์ ์์ ์ผ๋ก ์คํ๋์ง๋ง ์๋ก์ด task๋ฅผ ๋ฑ๋กํ๋ฉด ์๋์ ๊ฐ์ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ค.
unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard
ํด์ํด๋ณด๋ฉด ์คํ ๋ฆฌ๋ณด๋์ ์ ๊ณผ ์ฐ๊ฒฐ๋์ด ์์ง ์๋ค๋ ์ค๋ฅ์๋ค.
์คํ ๋ฆฌ๋ณด๋์์ Cell์ identifier๋ฅผ ํ์ธํด๋ณด๋ ์ค์ ๋์ด ์์ง ์์๊ณ ์ด๋ฅผ Cell๋ก ๋ณ๊ฒฝํด์คฌ๋๋ ์ ์์ ์ผ๋ก ์๋ํ๋ค.



์ฐธ๊ณ
- tableView(_:numberOfRowsInSection:)
Apple Developer Documentation
developer.apple.com
- dequeueReusableCell(withIdentifier:for:)
Apple Developer Documentation
developer.apple.com
'๐ป ๊ฐ๋ฐ > ์ค๋์ ์ฝ์ง' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
TableView๋ฅผ ์ฌ์ฉํ๋ฉด์ Cell์ ์ฌ์ฌ์ฉํ๊ธฐ ์ํด extension์ผ๋ก UITableViewDataSource๋ฅผ ์ฌ์ ์ํ๊ณ ๋น๋๋ฅผ ์งํํ๋ค.
extension ViewController : UITableViewDataSource {
func tableView(_ tableView : UITableView, numberOfRowsInSection section : Int) -> Int {
return self.tasks.count
}
func tableView(_ tableView : UITableView, cellForRowAt indexPath : IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for : indexPath)
let task = self.tasks[indexPath.row]
cell.textLabel?.text = task.title
return cell
}
}
์ฑ์ ์ ์์ ์ผ๋ก ์คํ๋์ง๋ง ์๋ก์ด task๋ฅผ ๋ฑ๋กํ๋ฉด ์๋์ ๊ฐ์ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ค.
unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard
ํด์ํด๋ณด๋ฉด ์คํ ๋ฆฌ๋ณด๋์ ์ ๊ณผ ์ฐ๊ฒฐ๋์ด ์์ง ์๋ค๋ ์ค๋ฅ์๋ค.
์คํ ๋ฆฌ๋ณด๋์์ Cell์ identifier๋ฅผ ํ์ธํด๋ณด๋ ์ค์ ๋์ด ์์ง ์์๊ณ ์ด๋ฅผ Cell๋ก ๋ณ๊ฒฝํด์คฌ๋๋ ์ ์์ ์ผ๋ก ์๋ํ๋ค.



์ฐธ๊ณ
- tableView(_:numberOfRowsInSection:)
Apple Developer Documentation
developer.apple.com
- dequeueReusableCell(withIdentifier:for:)
Apple Developer Documentation
developer.apple.com