UserDefaults ์ ์ฅํ๋ ์๊ธฐ
๋ทฐ๊ฐ ์ฌ๋ผ์ง ๋๋ง ์ ์ฅํ๊ฒ ๋๋ฉด ๋ง์ฝ ์ฌ์ฉ์๊ฐ ํ๋ฉด์ ๋ ๋ ค์ ์์ ๋ฒ๋ ธ์ ๋ ์ ์ฅ ์ ๋จ.
→ ๊ทธ๋ฅ ์์๋ก ํ์ํ ๋๋ง๋ค ์ ์ ๋ํด์ธ ์ ์ ์ฅํ๋ ๊ฒ ์ข์. ๊ทธ ์ ๋๋ ํจ์จ์ ๋ฌธ์ X
๋ทฐ ๊ฐฑ์ ์ ์ธ์ ํ๋ ๊ฒ ์ข์๊น?
viewWillAppear : ํ๋ฉด์ด ๋ฐ ๋๋ง๋ค ํธ์ถ ๋๋ ํจ์.
UserDefaults ๊น๋ํ๊ฒ ์ฐ๋ ๋ฒ
- ์ ์ ๋ํด์ธ ๊ฐ์ด ๋ค๋ฅธ ํ๋ฉด(๋ค๋ฅธ ํญ๋ฐ ๋ฑ)์์ ๋ณ๊ฒฝ๋๊ณ ํ์ฌ ํ๋ฉด ์์๋ ๋ฐ๋ก๋ฐ๋ก ์ ๋ฐ์ดํธ ๋ ๋ชจ์ต์ ํ์ธํ๋ ค๋ฉด viewWillAppear(_:)์์ ์ ์ ๋ํด์ธ ๊ฐ ๋ถ๋ฌ์์ค์ผํจ.
UserDefaults์ ์ ์ฅ ํ ์ ์๋ ๊ฐ
Int, String, Bool, Float, String Array
๊ทธ ์ธ์ enum, class ๋ฑ์ ์ธ์ฝ๋ฉ์ด๋ ๋์ฝ๋ฉ ๋ฑ์ ๊ณผ์ ํ์
์ฑ์ด ์ฐจ์งํ๋ ๋ฐ์ดํฐ ๊ณต๊ฐ
Bundle Container : ์ฑ์คํ ์ด์์ ์ค์น ํ ๋ ๋จ๋ ์ฉ๋ → ์ฑ ์ค์นํ ๋ ํ์ํ ์ฉ๋
Data Container → ์์ดํฐ ์ค์ -์ ์ฅ ๊ณต๊ฐ์์ ๋ฌธ์ ๋ฐ ๋ฐ์ดํฐ์ ๋จ๋ ์ฉ๋
Data container
Documents,Library๊ฐ ์ฐ๋ฆฌ๊ฐ ๊ฑด๋๋ ์ง์ . Documents๋ฅผ ๊ฐ์ฅ ๋ง์ด ๋ค๋ฃธ.(ํ์ผ ์ฑ์์ ์ฌ์ฉํ๋ ๋ถ๋ถ๋ Documents)
SystemData, Library๋ ๊ฑด๋ค์ผ ๊ฑฐ์ ์๋ ๋ถ๋ถ.
1. ํ ์ด๋ธ ๋ทฐ ๊ตฌํํ๊ธฐ
UIViewController์ ๋ฃจํธ๋ทฐ → UIView
UITableViewController์ ๋ฃจํธ๋ทฐ → UITableView
1. TableViewController์ Tableview
- TableView ํฌ๊ธฐ์กฐ์ ์ ๋จ
- TableView cell ํฌ๊ธฐ์กฐ์ ๋จ
tableviewdp dataSource,delegate ๋ฏธ๋ฆฌ ์ฐ๊ฒฐ๋์ด์์
UIView์์ TableView์ TableViewCell ์น๊ธฐ
TableView๋ ํฌ๊ธฐ ์กฐ์ ๊ฐ๋ฅํจ.
TableviewCell
- system: ์ ํ์ด ๊ธฐ๋ณธ ์ ๊ณตํ๋ cell
- :contents + accesory๋ก ๊ตฌ์ฑ
- custom: ์ง์ custom ํ ์ ์๋ cell
์ ํ์ด ์ ๊ณตํ๋ cell ์คํ์ผ
Accessory ๊ฐ์๊ฑฐ๋ ์์
TableView
์ข ๋ฅ
- Dynamic : ์ฌ์ฉ์์ ๋ฐ๋ผ ๋ด์ฉ๊ณผ ๊ธธ์ด๊ฐ ๋ฌ๋ผ์ง๋ ํ ์ด๋ธ์ ์ฌ์ฉ
- Static: ๋ชจ๋ ์ฌ์ฉ์์๊ฒ ๊ฐ์ ๊ฒ์ ๋ณด์ฌ์ฃผ๋ ํ ์ด๋ธ์ ์ฌ์ฉ (ex.์ค์ )
Header,Footer
ํค๋ํธํฐ ํ๋๋ง ๊ฐ์ง ์ ์์ ํ ์ด๋ธ ๋ทฐ ์ ์๋์ชฝ ui ๊ตฌ์ฑํ๊ณ ์ถ์ ๋ .
uiview๋ฅผ ๋์ด๋ค๊ฐ ํ ์ด๋ธ ๋ทฐ์ ์์๋ ๋ฃ์ผ๋ฉด ์๋์ผ๋ก ์ฝ ๋ค์ด๊ฐ.
Style
- Plain
- Grouped
- Inset Grouped (iOS 13~)
Section
ํ ์ด๋ธ๋ทฐ๊ฐ ๊ทธ๋ฃน๋ณ๋ก ๋๋์ด์ ธ ์์.
separator inset ๋ณ๊ฒฝ
์ด๊ฒ์ ๊ฒ ํด๋ณด๊ธฐ - Static TableView
cell style์ด custom์ผ๋ก ๋์ด์์ด์ผ ์ฌ๋ฌ๊ฐ์ง uiview์์ ๋ง๋๋ก ์ฌ๋ฆด ์ ์์.
TableView - Selection ๋ช๊ฐ ์ ํํ ์ง
ํ ์ด๋ธ ๋ทฐ๋ฅผ ์ํด ๊ตฌํํด์ผ ํ ์ต์ํ์ ํจ์
1.์น์ ๋ด ์ ์ ๊ฐฏ์ : ์นดํก ์น๊ตฌ ์๋งํผ ์ ๊ฐฏ์๊ฐ ํ์ํด ๋ผ๊ณ iOS ์๊ฒ ์ ๋ฌ. ์ฒ๊ฐ ๋ง๊ฐ ํด๋ ๋จ (์ด์ฐจํผ ํ๋ฉด์ ๋ณด์ด๋ ์ ๊ฐ์๋งํผ๋ง ์ค๋นํด๋๊ณ ์ฌ์ฌ์ฉํจ)
override func numberOfSections(in tableView: UITableView) -> Int {
return 100
}
2.์ ๋์์ธ ๋ฐ ๋ฐ์ดํฐ ์ฒ๋ฆฌ : ์นดํก ํ๋กํ ๋ชจ์๋ฆฌ ๋ฅ๊ธ๊ฒ, ํ๋กํ ์ด๋ฏธ์ง์ ์ํ ๋ฉ์์ง ๋ฐ์ ๋ฑ
ํ๋์ ํ ์ด๋ธ ๋ทฐ์ ์ฌ๋ฌ๊ฐ์ง ์ข ๋ฅ์ ์ ์ด ๋ค์ด ๊ฐ ์ ์์ด์ ์ ์ ์ด๋ฆ(identifier)์ด ํ์
Cell Identifier ์ง์ ๋ฐฉ๋ฒ
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SettingCell")!
cell.textLabel?.text = "ํ
์คํธ"
cell.textLabel?.textColor = .red
cell.textLabel?.font = .boldSystemFont(ofSize: 28)
cell.detailTextLabel?.text = "๋ํ
์ผ ํ
์คํธ"
cell.detailTextLabel?.textColor = .blue
cell.detailTextLabel?.font = .systemFont(ofSize: 15)
cell.imageView?.image = UIImage(systemName: "star.fill")
return cell
}
- ์ ์ ๋์ด (ํ์๋ ์๋ ์ํ๋ฉด ๋ํดํธ๊ฐ)
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 70
}
๋์ค์ ์์ด์ง๋๊น ์์ ํด์ผ ํ ๋ถ๋ถ.
heghtForRowAt ํจ์๋ก ๋์ด ์ง์ ํ๋ฉด ์ ํ๋ํ๋ ๋ง๋ค ์๊ธธ๋๋ง๋ค ํจ์ ํธ์ถ ๋จ
๋ชจ๋ ์ ๋์ด๊ฐ ๊ฐ์ด ๊ฐ์ผ๋ฉด ๋ค์๊ณผ ๊ฐ์ด ์ง์ ํ ์ ๋ ์์.
์น์ ๋ณ๋ก ๋ค๋ฅด๊ฒ ์ง์ ํ๋ ๋ฐฉ๋ฒ
import UIKit
class StudyTableViewController: UITableViewController {
let studyList = ["๋ณ์","์์","์ด๊ฑฐํ","์ต์
๋ ๋ฐ์ธ๋ฉ", "๋ฉ์๋", "ํ๋กํผํฐ"]
override func viewDidLoad() { super.viewDidLoad() }
override func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return section == 0 ? "์ฒซ๋ฒ์งธ ์น์
" : "๋๋ฒ์งธ ์น์
"
}
//1. ์
๊ฐฏ์ (ํ์)
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return section == 0 ? studyList.count : 3
}
//2. ์
๋ฐ์ดํฐ ๋ฐ ๋์์ธ ์ฒ๋ฆฌ (ํ์)
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "todoCell")!
if indexPath.section == 0 {
cell.textLabel?.text = studyList[indexPath.row]
}else {
cell.textLabel?.text = "๋๋ฒ์งธ ์น์
์ ๋ค์ด๊ฐ ํ
์คํธ"
}
return cell
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 40
}
textLabel ๋ค์ ?๊ฐ ๋ถ๋ ์ด์
textLabel์๋ ?๊ฐ ๋ถ์ด ์๊ณ textLabel์ด ์กด์ฌ ํด์ผ ๋ค์ ์ฝ๋ text = studyList[indexPath.row]๊ฐ ์คํ ๋จ.
Basic style Cell์ detailTextLabel์ด ์์
Style์ ๋ฐ๋ผ ์ด๋ฏธ์ง๋ ์์ ์๋ ์๊ณ ์์ ์๋ ์์.
๋ค๋น๊ฒ์ด์ ์ปจํธ๋กค๋ฌ ์๋ฒ ๋ํ๋ฉด ๋ค๋น๊ฒ์ด์ ๋ฐ ์๋ ์๊น.
๋ค๋น๊ฒ์ด์ ์ UIButton ๋ฃ์ผ๋ฉด ๋์ ์ ํจ . Bar Button ๋ฃ์ด์ผ ๋์ ํจ.
reloadData()
ํ ์ด๋ธ๋ทฐ ๊ฐฑ์ ํด์ฃผ๋ ํจ์. ํ ์ด๋ธ ๋ทฐ ์ฒ์๋ถํฐ ๋ค์ ๊ทธ๋ ค์ค
๋ญํ๋ ๋ฐ๋๋ฉด ๊ณ์ ๊ฐฑ์ ํด ์ค. ๋ฐ์ดํฐ๋ ๋ทฐ๊ฐ ๋ฐ๋ก ๋๊ธฐ ๋๋ฌธ์ ์ฑํฌ ํด์ฃผ๋ ๊ณผ์ ์ด ํ์ํจ(๋๊ธฐํ)
reload ์ข ๋ฅ ์ฌ๋ฌ๊ฐ์ ์ ์ ํ ํจ์จ์ ์ผ๋ก ํ๊ธฐ ์ํด ๊ฐ ํจ์๋ฅผ ์ฌ์ฉํ ์ ์์
Extension
๊ฐ์ ๋์์ธ์ด๋ ํฐํธ ๋ฃ๊ณ ์ถ์ผ๋ฉด ์ต์คํ ์ ์ผ๋ก ๋นผ์ฃผ๋ฉด ์ข์.
ํ ์ด๋ธ ๋ทฐ์๋ ํ ์ด๋ธ ๋ทฐ๋น ํ๋๋ง ๊ฐ์ง ์ ์๋ tableHeaderView,tableFooterView(UIView) ํ๋กํผํฐ์
์น์ ๋ง๋ค ํ๋์ฉ ๊ฐ์ง ์ ์๋ headerView, footerView(UITableViewHeaderFooterView) ๊ฐ ์๋ค.
์น์ ๋ง๋ค ๊ฐ์ง๋ headerView๋ ์๋์ผ๋ก sticky header
'SeSAC' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[iOS] TIL_230731 (0) | 2023.08.03 |
---|---|
[iOS] TIL_230728 (0) | 2023.08.01 |
[iOS] TIL_230726 (0) | 2023.07.31 |
[iOS] TIL_230725 (0) | 2023.07.30 |
[iOS] TIL_230719 (0) | 2023.07.25 |