๐Ÿ“ฑ App Development Study/iOS๐ŸŽ

[Udemy iOS & Swift Bootcamp] Todo App(Local Data Persistance)

ibelieveinme 2023. 7. 18. 15:50
728x90

Local ์— ๋ฐ์ดํ„ฐ๊ฐ€ ์ €์žฅ๋˜๋Š” ToDo List ์•ฑ์„ ๋งŒ๋“ค์–ด๋ณด์ž.


1. Project Clone

 

GitHub - appbrewery/Todoey-iOS13: Learn to Code While Building Apps - The Complete iOS Development Bootcamp

Learn to Code While Building Apps - The Complete iOS Development Bootcamp - GitHub - appbrewery/Todoey-iOS13: Learn to Code While Building Apps - The Complete iOS Development Bootcamp

github.com

 

2. Table View Controller ๋งŒ๋“ค๊ธฐ

Table View Controller ์ถ”๊ฐ€ํ•˜๊ธฐ. Controller ๋กœ ์ถ”๊ฐ€ํ•˜๋ฉด Component ๊ฐ€ ์ถ”๊ฐ€๋˜๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ ํ™”๋ฉด ํ•˜๋‚˜๊ฐ€ ์ถ”๊ฐ€ ๋œ๋‹ค.

 

Prototype table cells must have reuse identifiers. 

ํ˜น์‹œ ์œ„์™€ ๊ฐ™์ด prototype์ด identifier๋ฅผ ๋ฐ˜๋“œ์‹œ ์žฌ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค๋Š”(์ฝ”๋“œ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค๋Š”) ๊ฒฝ๊ณ ๊ฐ€ ๋œจ๋ฉด Table View Cell(Prototype ๋ถ€๋ถ„)์—  identifier ์„ ์ž…๋ ฅํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

3. Initial View ์ง€์ •

๊ธฐ์กด View ๋Š” ์ œ๊ฑฐํ•˜๊ณ  ๋ฐฉ๊ธˆ ๋งŒ๋“  Table View Controller ๋ฅผ inital View ๋กœ ํ™”์‚ดํ‘œ ์ง€์ •ํ•ด์ค€๋‹ค.

๋งŒ์•ฝ ํ™”์‚ดํ‘œ ํ†ต์งธ๋กœ ๋‚ ๋ ค๋ฒ„๋ ธ๋‹ค๋ฉด ์†์„ฑ inspector ์—์„œ Is Initial View Controller ์ฒดํฌํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

4. Navigation Controller ์ถ”๊ฐ€ํ•˜๊ธฐ

Editor > Embed In > Navigation Controller ๋ฅผ ์„ ํƒํ•˜๋ฉด ์š”๋ ‡๊ฒŒ Navigation Controller ๊ฐ€ ์ถ”๊ฐ€๋œ๋‹ค.

 

5. Navigation Top Bar ๊พธ๋ฏธ๊ธฐ

Navigation Top Bar ์— Title ์ง€์ •ํ•˜๊ธฐ.

 

Navigation Bar ์— ์ƒ‰์ƒ ๋ฐ Title ์ปฌ๋Ÿฌ ์ง€์ •ํ•˜๊ธฐ. (Bar Tint ๋Š” System Blue Color, Title color ๋Š” White Color)

๋Š” ์•ˆ๋˜๋„ค ? Bar Tint ๋ฅผ System Blue Color ๋กœ ์ ์šฉํ–ˆ๋Š”๋ฐ ๋ฐ˜์˜์ด ์•ˆ๋œ๋‹ค.

Background, Tint ์ปฌ๋Ÿฌ๋ฅผ System Blue Color ๋กœ ๋ชจ๋‘ ๋ณ€๊ฒฝํ•˜๋‹ˆ ๋ฐ˜์˜๋๋‹ค.

 

6. ViewController ์ฝ”๋“œ ํŒŒ์ผ ์ˆ˜์ •ํ•˜์—ฌ tableViewController ์™€ ์—ฐ๊ฒฐ

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

import UIKit

class TodoListViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

๊ธฐ๋ณธ์œผ๋กœ ์ฃผ์–ด์ง„ ViewController ๋ฅผ TodoListViewController ๋กœ ์ด๋ฆ„์„ ๋ณ€๊ฒฝํ•œ๋‹ค. ํŒŒ์ผ๋ช…๋„ ๋ณ€๊ฒฝํ•ด์ค˜์•ผ ํ•จ!

 

์œ„์—์„œ ๋งŒ๋“  TableViewController๋ฅผ ์—ฐ๊ฒฐํ•ด์„œ ์‚ฌ์šฉํ•  ํด๋ž˜์Šค ์ด๋ฏ€๋กœ UIViewController ๊ฐ€ ์•„๋‹Œ UITableViewController ๋ฅผ ์ƒ์†๋ฐ›์•„์ค€๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด TableView๋ฅผ ๋ณ€์ˆ˜๋กœ ์„ ์–ธํ•ด์ค„ ํ•„์š” ์—†์ด ์ž๋™์œผ๋กœ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๋‹ค.

 

tableViewController ์˜ ์”ฌ์ด TodoListVIewController ํด๋ž˜์Šค์™€ ์ž˜ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.

์—ฐ๊ฒฐ์ด ์•ˆ๋˜์–ด ์žˆ๋‹ค๋ฉด ํด๋ž˜์Šค๋ฅผ ์ง์ ‘ ์„ ํƒํ•ด์ค€๋‹ค.

 

7. Table View ๋ฆฌ์ŠคํŠธ ๋‚ด์šฉ ํ•˜๋“œ ์ฝ”๋“œ๋กœ ํ™•์ธํ•˜๊ธฐ

import UIKit

class TodoListViewController: UITableViewController {

    let itemArray = ["Find Mike", "Buy Eggs", "Destroy Demogorgon"]
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return itemArray.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TodoItemCell", for: indexPath)
        cell.textLabel?.text = itemArray[indexPath.row]
        return cell
    }
}

tableView ์— ๊ฐ’์„ ๋ฟŒ๋ฆฌ๊ธฐ ์œ„ํ•œ ํ•จ์ˆ˜๋“ค์„ override ํ•ด์„œ ์ž‘์„ฑํ•ด์ค€๋‹ค. (์ฑ„ํŒ…์•ฑ ๋งŒ๋“ค๊ธฐ์—์„œ ๋ฐฐ์› ๋˜ ๋‚ด์šฉ. ๋งค์šฐ ๊นŒ๋จน์Œใ…Ž.)

numberOfRowsInSection ๊ฐ€ ์žˆ๋Š” ํ•จ์ˆ˜๋กœ row ๊ฐœ์ˆ˜ return ํ•˜๊ณ , cellForRowAt ๊ฐ€ ์žˆ๋Š” ํ•จ์ˆ˜๋กœ cell ์— ๊ฐ’์„ ๋‹ด๋Š”๋‹ค.

 

*์ฐธ๊ณ 

TableView ์˜ list ๋ฅผ ๋ฟŒ๋ฆฌ๋Š” ํ•จ์ˆ˜๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์•„๋ž˜ ๋‘ ํ•จ์ˆ˜๋ฅผ override ํ•œ๋‹ค.

 //๋งŒ๋“ค์–ด์•ผ ํ•  cell ๊ฐœ์ˆ˜ ๋ฐ˜ํ™˜
 func tableView(UITableView, numberOfRowsInSection: Int) -> Int
//์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ table view cell์„ ์ƒ์„ฑํ•ด์„œ ๋ฐ˜ํ™˜
func tableView(UITableView, cellForRowAt: IndexPath) -> UITableViewCell

 

8. ์‹คํ–‰๊ฒฐ๊ณผ ํ™•์ธ

์‹คํ–‰ํ™”๋ฉด. 3๊ฐœ์˜ ๋ฐ์ดํ„ฐ๊ฐ€ 3๊ฐœ ํ–‰์œผ๋กœ ์ž˜ ๋‚˜์˜จ๋‹ค.

 

 

728x90