TableView ๋ฅผ ์ํ Protocol
UITableView ๋ฅผ ์ํ Protocol ์ UITableViewDataSource, UITableViewDelegate ๊ฐ ์๋ค. ์ฌ๊ธฐ์ Protocol ์ TableView๋ฅผ ์ด์ฉํ๊ธฐ ์ํด ํด์ผํ ์ผ๋ค์ ๋ชฉ๋ก, ์ฆ ๊ตฌํํด์ผํ method ๋ค์ด๋ค.
protocol ์ค์๋ ํด๋์ค๊ฐ ์์์ ํ๋ฏ์ด ์์ฑํด์ฃผ๋ฉด ๋๋ค. UITableViewDataSource protocol ์ ๊ฒฝ์ฐ, ํ์ ๊ตฌํ ํจ์๋ฅผ ์ถ๊ฐํ์ง ์์ผ๋ฉด, ์๋์ ๊ฐ์ ์๋ฌ๊ฐ ๋ฐ์ํ๋ค. Fix ๋ฅผ ๋๋ฅด๋ฉด ์๋์ผ๋ก ์ถ๊ฐํด์ค๋ค.
extension BountyViewController: UITableViewDataSource {
}

UITableViewDataSource
protocol UITableViewDataSource
๐ก The methods adopted by the object you use to manage data and provide cells for a table view.
Table view data source object ๋ ํด๋น ํ๋กํ ์ฝ์ ์ฑํํ๋ค. Table view ๋ ๋ฐ์ดํฐ ๊ธฐ๋ฐ์ด๊ณ , ์ผ๋ฐ์ ์ผ๋ก ์ ๊ณต๋ data source object ๋ฅผ ํตํด ๋ฐ์ดํฐ๋ฅผ ์ป๋๋ค. data source object ๋ ์ฑ์ ๋ฐ์ดํฐ๋ฅผ ๊ด๋ฆฌํ๊ณ , table ์ cell ์ ์์ฑํ๊ณ ๊ด๋ฆฌํ ์ฑ ์์ ๊ฐ๋๋ค.
Data source object ์ ์ฑ ์์ ๋ค์๊ณผ ๊ฐ๋ค.
- table ์ section ๊ณผ row ์ ๊ฐ์๋ฅผ ์๋ ค์ฃผ๋ ๊ฒ
- table ์ ๊ฐ row ์ cell ์ ์ ๊ณตํ๋ ๊ฒ
- section header ์ footer ์ title ์ ์ ๊ณตํ๋ ๊ฒ
- table ์ index ๋ฅผ ์ค์ ํ๋ ๊ฒ
- ๊ธฐ๋ณธ ๋ฐ์ดํฐ๋ฅผ ๋ณ๊ฒฝํด์ผ ํ๋ user ๋๋ table ์ด๊ธฐ ์ ๋ฐ์ดํธ์ ์๋ตํ๋ ๊ฒ
TableView ๋ฅผ ์ด์ฉํ๊ธฐ ์ํด์ ํ์๋ก ๊ตฌํํด์ผ ํ๋ method ๊ฐ ์๋ค. TableView ์ cell ์ด ๋ช๊ฐ์ธ์ง? Table view ๋ฅผ ์ด๋ป๊ฒ ๋ณด์ฌ์ค ๊ฒ์ธ์ง? ์ ๋ํ ์ง๋ฌธ์ด๋ค. ํด๋น ์ง๋ฌธ์ ๋ค์ method ๋ฅผ ๊ตฌํํจ์ผ๋ก ๋ต๋ณํ ์ ์๋ค.
1. tableView(_:numberOfRowsInSection:)
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
- ํน์ section ์ ํ์ํ row ์ ๊ฐ์๋ฅผ ๋ฌป๋ method
- return ๊ฐ์ผ๋ก ๋ช ๊ฐ์ ์ ์ ๋ณด์ฌ์ค์ง์ ๋ํ ๋ต๋ณ์ ๊ตฌํํ๋ฉด ๋๋ค.
- section ์ ๋ฐ๋ผ ๋ค๋ฅธ ์ ์ ๊ฐ์๋ฅผ ๋ฐํํ๋๋ก ๊ตฌํํ ์ ์๋ค.
2. tableView(_:cellForRowAt:)
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
- ํน์ ์์น์ ํ์ํ cell ์ ์์ฒญํ๋ method
- cell ์ ์์ฑํ ๋, custom cell ์ ์ฌ์ฌ์ฉํ๋ ค๋ฉด dequeueReusableCell(withIdentifier:for:) method ๋ฅผ ์ด์ฉํ๋ค.
- cell ์ ๋ง๋ ํ์, data ๊ฐ๋ค์ ํตํด cell ์ ์์ฑ๋ค์ ์ ๋ฐ์ดํธํ ์ ์๋ค.
- return ๊ฐ์ผ๋ก UITableViewCell ์ ์์๋ฐ์ object ๋ฅผ ๋ฐํํด์ผ ํ๋ค. (nil ๋ฐํ์ ์๋ฌ ๋ฐ์)
UITableViewDelegate
protocol UITableViewDelegate
๐กMethods for managing selections, configuring section headers and footers, deleting and reordering cells, and performing other actions in a table view.
Table view delegate object๋ ํด๋น ํ๋กํ ์ฝ์ ์ฑํํ๋ค. Table view ๋ด์ ํด๋ฆญ๋ ์ ์ ๋งค๋์งํ๊ฑฐ๋, ์น์ ์ header์ footer ์ค์ , ์ ์ ์ญ์ ํ๊ฑฐ๋ ์ฌ์ก๋ดํ๊ธฐ ์ํด ์ด์ฉํ๋ค.
ํด๋น ํ๋กํ ์ฝ์ method ๋ค์ ๋ค์ ๊ธฐ๋ฅ๋ค์ ์ ๊ณตํ๋ค.
- custom header ์ footer view ๋ฅผ ๋ง๋ค๊ณ ๊ด๋ฆฌํ๋ ๊ฒ
- row, header, footer ์ ๋์ด๋ฅผ ์ง์ ํ๋ ๊ฒ
- ๋ ๋์ ์คํฌ๋กค ์ง์์ ์ํด ๋์ด์ ์ถ์ ์น๋ฅผ ์ ๊ณตํ๋ ๊ฒ
- row ์ ๋ค์ฌ์ฐ๊ธฐ ์์ค์ ์ง์ ํ๋ ๊ฒ
- ์ ํ๋ row ์ ๋ํด ์๋ตํ๋ ๊ฒ
- table row ๋ฅผ ์ค์์ดํํ๋ ๋ฑ ์ก์ ์ ์๋ตํ๋ ๊ฒ
- table content ๋ฅผ ํธ์งํ ์ ์๋๋ก ์ง์ํ๋ ๊ฒ
UITableViewDelegate ํ๋กํ ์ฝ์ ํ์ ๊ตฌํ method ๊ฐ ์๋ค. ๋ค์์ ์ฃผ๋ก ์์ฑํ๋ method ์ด๋ค.
tableView(_didSelectRowAt:)
optional func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
- delegate (๋๋ฆฌ์ธ)์๊ฒ row๊ฐ ์ ํ๋์์์ ์๋ ค์ฃผ๋ ํจ์.
- ์ ์ ์ ํํ์ ๋ ์ด๋ป๊ฒ ํ ์ง์ ๋ํด ๊ตฌํํ ์ ์๋ ํจ์์ด๋ค.
- ์์๋ก, TableView ๊ฐ List page ์ด๊ณ Detail page ๊ฐ ์๋ค๋ฉด ํด๋น method ์์ performSegue ๋ฅผ ํธ์ถํด Detail page ๋ก ๋์ด๊ฐ๋ ์ฝ๋๋ฅผ ์์ฑํ ์ ์๋ค.
extension ListViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("--> \(indexPath.row)")
performSegue(withIdentifier: "showDetail", sender: indexPath.row)
}
}
๊ณต์ ๋ฌธ์
- Apple Developer Documentation - UITableViewDataSource
- Apple Developer Documentation - UITableViewDelegate
์ฐธ๊ณ ๋ฌธ์
- woonhyeong.tistory.com/6?category=827228 > Datasource, Delegate method ๊ฐ ์ ์ ๋ฆฌ๋์ด ์๋ค.
'๐ iOS > UIKit' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[iOS/Swift] ์จ๋ณด๋ฉ(Onboarding) ํ์ด์ง ๋ง๋ค๊ธฐ - Storyboard, UserDefaults, SceneDelegate (0) | 2021.03.08 |
---|---|
[iOS/Swift] TableView ๋ก ์ด๊ฐ๋จ TodoList ๋ง๋ค๊ธฐ (2) | 2021.03.01 |
[iOS/Swift] TableView ์ ๊ธฐ์ด :: UITableView, UITableViewCell (0) | 2021.03.01 |
[iOS/Swift] Launch Screen :: ์ฑ์ ์์ ํ๋ฉด (0) | 2021.02.21 |
[iOS/Swift] Tab Bar - UITabBar, UITabBarController (4) | 2021.02.14 |