๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
SeSAC

[iOS] TIL_230727

by lizzydev 2023. 7. 31.

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  
}
  1. ์…€์˜ ๋†’์ด (ํ•„์ˆ˜๋Š” ์•„๋‹˜ ์•ˆํ•˜๋ฉด ๋””ํดํŠธ๊ฐ’)
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