๐ ๋ชฉ์ฐจ
-
- ํ๋กํ ์ฝ(protocol)์ด๋?
-
- Delegate ํจํด ์ดํด
-
- DataSource vs Delegate ์ฐจ์ด
-
- UIPickerView ๊ตฌํ ํต์ฌ
-
- TableView ํต์ฌ ๊ตฌ์กฐ
-
- ์ค๋ฌด์์ ์ค์ํ ํฌ์ธํธ
๐ 1. ํ๋กํ ์ฝ(protocol)์ด๋?
โ ์ ์
ํ๋กํ ์ฝ์ **ํด๋์ค๊ฐ ๋ฐ๋์ ๊ตฌํํด์ผ ํ ๊ธฐ๋ฅ ๋ชฉ๋ก(์ค๊ณ๋)**์ ๋๋ค.
๐ ์ฝ๊ฒ ๋งํ๋ฉด
๐ “์ด ๊ธฐ๋ฅ๋ค ๋ฐ๋์ ๋ง๋ค์ด๋ผ!” ํ๋ ๊ท์น
๐ ํน์ง
- ๋ฉ์๋ “์ ์ธ๋ง” ์กด์ฌ (๊ตฌํ ์์)
- ํด๋์ค/๊ตฌ์กฐ์ฒด/์ด๊ฑฐํ์์ ์ฑํ ๊ฐ๋ฅ
- Java์ interface์ ๋์ผ ๊ฐ๋
๐ ์์ ์ฝ๋
protocol Runnable {
var x: Int { get set }
func run()
}
๐ ๊ตฌํ (์ฑํ + ์ค์)
class Man: Runnable {
var x: Int = 1
func run() {
print("๋ฌ๋ฆฐ๋ค~")
}
}
๐ ๊ตฌํ ์ ํ๋ฉด?
โ ์๋ฌ ๋ฐ์
๐ “protocol์ ์ค์ํ์ง ์๋๋ค”
๐ 2. Delegate ํจํด ์ดํด
โ ๊ฐ๋ ํ๋ฐฉ ์ ๋ฆฌ
- protocol ๐ ํด์ผ ํ ์ผ ๋ชฉ๋ก (์๋ฐ ๊ณต๊ณ )
- delegate ๐ ์ค์ ์ผํ๋ ์ฌ๋ (์๋ฐ์)
๐ ํต์ฌ ๊ฐ๋
๐ “์ผ์ ๋์ ์ฒ๋ฆฌํด์ฃผ๋ ๊ตฌ์กฐ”
๐ ํ ๊ฐ์ฒด๊ฐ ๋ชจ๋ ์ผ์ ํ์ง ์๊ณ
๐ ์ผ๋ถ ๊ธฐ๋ฅ์ ๋ค๋ฅธ ๊ฐ์ฒด์๊ฒ ์์
๐ ์๋ฃ ์ค๋ช (4~5ํ์ด์ง)
- delegate = ๋๋ฆฌ์, ์กฐ๋ ฅ์
- ํน์ ์ด๋ฒคํธ ๋ฐ์ ์ ๋์ ์ฒ๋ฆฌ
๐ 3. DataSource vs Delegate ์ฐจ์ด
์ด๊ฑฐ ์ํ ํต์ฌ๐ฅ
๊ตฌ๋ถ์ญํ
| DataSource | ๋ฐ์ดํฐ๋ฅผ ์ ๊ณต |
| Delegate | ์ด๋ฒคํธ ์ฒ๋ฆฌ |
โ ์ฝ๊ฒ ์ดํด
- DataSource ๐ “๋ญ ๋ณด์ฌ์ค๊น?”
- Delegate ๐ “์ฌ์ฉ์๊ฐ ๋ญ ํ์ ๋ ์ด๋ป๊ฒ ํ ๊น?”
๐ 4. UIPickerView ํต์ฌ ๊ตฌํ
โ ๋ฐ๋์ ํด์ผ ํ ๊ฒ
1๏ธโฃ ํ๋กํ ์ฝ ์ฑํ
class ViewController: UIViewController,
UIPickerViewDelegate,
UIPickerViewDataSource {
2๏ธโฃ delegate ์ค์
pickerView.delegate = self
pickerView.dataSource = self
โ ํ์ ๋ฉ์๋ (DataSource)
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView,
numberOfRowsInComponent component: Int) -> Int {
return data.count
}
๐ ์ด๊ฑด ๋ฌด์กฐ๊ฑด ๊ตฌํํด์ผ ํจ
โ ์ ํ ๋ฉ์๋ (Delegate)
func pickerView(_ pickerView: UIPickerView,
didSelectRow row: Int,
inComponent component: Int) {
print("์ ํ๋จ")
}
๐ ํ์ํ ๋๋ง ๊ตฌํ
๐ 5. TableView ํต์ฌ ๊ตฌ์กฐ
โ TableView๋?
๐ “๋ฐ์ดํฐ๋ฅผ ํ ํํ๋ก ๋ณด์ฌ์ฃผ๋ UI”
โ ํ์ ๋ฉ์๋ (์ํ ์ค์๐ฅ)
override func tableView(_ tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
return items.count
}
๐ ํ ๊ฐ์ ๋ฐํ
override func tableView(_ tableView: UITableView,
cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(
withIdentifier: "cell",
for: indexPath)
cell.textLabel?.text = "๋ฐ์ดํฐ"
return cell
}
๐ ์ ์์ฑ + ๋ฐ์ดํฐ ํ์
โ Delegate ์ฃผ์ ๊ธฐ๋ฅ
func tableView(_ tableView: UITableView,
didSelectRowAt indexPath: IndexPath) {
print("ํด๋ฆญ๋จ")
}
๐ ์ฌ์ฉ์ ํด๋ฆญ ์ฒ๋ฆฌ
๐ 6. ์ค๋ฌด ํต์ฌ ํฌ์ธํธ
์๋ฃ 2ํ์ด์ง ํต์ฌ ์์ฝ๐ฅ
โ iOS ํ์ ๊ธฐ์
- TableView
- ๋คํธ์ํฌ ํต์ (REST API)
- JSON ํ์ฑ
- ๋น๋๊ธฐ ์ฒ๋ฆฌ
โ ์ฑ ๊ฐ๋ฐ ํ๋ฆ
- Open API ์์ฒญ
- JSON ๋ฐ์ดํฐ ๋ฐ๊ธฐ
- ํ์ฑ
- TableView์ ์ถ๋ ฅ
๐ ์ด๊ฒ ์ค๋ฌด ๊ธฐ๋ณธ ๊ตฌ์กฐ
โ ์ ๋ฆฌ (ํต์ฌ ์์ฝ)
โ protocol = ๊ธฐ๋ฅ ์ค๊ณ๋
โ delegate = ์ด๋ฒคํธ ์ฒ๋ฆฌ ๋ด๋น
โ datasource = ๋ฐ์ดํฐ ๊ณต๊ธ
โ TableView ํต์ฌ 2๊ฐ
- numberOfRows
- cellForRow
โ iOS ์ฑ ํต์ฌ ํ๋ฆ
๐ API → JSON → ํ์ฑ → UI ์ถ๋ ฅ
'iOS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [iOS] 7์ฃผ์ฐจ ์ด๊ฑฐํ & ๊ตฌ์กฐ์ฒด ์ ๋ฆฌ (0) | 2026.04.20 |
|---|---|
| [iOS] ์ต์ ๋ ์ฒด์ด๋, ์ค๋ฅ ์ฒ๋ฆฌ, ์ ๋ค๋ฆญ, Array ์์ ์ ๋ฆฌ (0) | 2026.04.13 |
| [iOS] iOS ํ๋ก๊ทธ๋๋ฐ 4์ฃผ์ฐจ ์ ๋ฆฌ (0) | 2026.03.30 |
| [iOS] Swift ๋ฐ์ดํฐ ํ์ ยท Tuple ยท Optional ยท ์ฐ์ฐ์ ยท ์ ์ด๋ฌธ ์ ๋ฆฌ (0) | 2026.03.16 |
| [iOS]BMI ์ฑ ๋ง๋ค๊ธฐ 3์ฃผ์ฐจ (0) | 2025.11.25 |