Skip to content

Latest commit

ย 

History

History
166 lines (103 loc) ยท 6.37 KB

UITableView.md

File metadata and controls

166 lines (103 loc) ยท 6.37 KB

UITableView

๋ฐ์ดํ„ฐ๋ฅผ ๋ชฉ๋ก ํ˜•ํƒœ๋กœ ๋ณด์—ฌ์ค„ ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ UI Component

โ†’ UIScrollView๋ฅผ ์ƒ์†๋ฐ›๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋งŽ์€ ์ปจํ…์ธ ๋ฅผ ๋ณด์—ฌ์ค„ ์ˆ˜ ์žˆ์Œ


  • ์—ฌ๋Ÿฌ ๊ฐœ์˜ Cell์„ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ  ํ•˜๋‚˜์˜ ์—ด๊ณผ ์—ฌ๋Ÿฌ ํ–‰์„ ์ง€๋‹ˆ๊ณ  ์žˆ์œผ๋ฉฐ, ์ˆ˜์ง์œผ๋กœ๋งŒ ์Šคํฌ๋กค ๊ฐ€๋Šฅ

  • Section์„ ํ†ตํ•ด ํ–‰์„ ๊ทธ๋ฃนํ™”ํ•˜์—ฌ ์ฝ˜ํ…์ธ ๋ฅผ ์ข€ ๋” ์‰ฝ๊ฒŒ ํƒ์ƒ‰ ๊ฐ€๋Šฅ

  • Section์˜ Header, Footer์— View๋ฅผ ๊ตฌ์„ฑํ•ด ์ถ”๊ฐ€์ ์ธ ์ •๋ณด ํ‘œ์‹œ ๊ฐ€๋Šฅ


More

Controller๊ฐ€ Model๊ณผ View๋ฅผ ์—ฐ๊ฒฐํ•ด์ฃผ๋Š”๋ฐ, controller์—์„œ model, view๋กœ๋Š” ์ž์œ ๋กญ๊ฒŒ ์ ‘๊ทผ๊ฐ€๋Šฅํ•˜์ง€๋งŒ View์—์„œ Controller๋กœ์˜ ์†Œํ†ต์€ ์ œํ•œ์ ์ด๋‹ค. ์ฆ‰, View์—์„œ Controller์—๊ฒŒ View์˜ ์ •๋ณด ๋ฐ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ๋งํ•ด์ค„ ๋•, Delegate์™€ DataSource๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์˜์‚ฌ์†Œํ†ต์„ ์ง„ํ–‰ํ•œ๋‹ค.

  • Delegate

    • ํ•ด์•ผํ•  ์ผ์„ ์œ„์ž„ํ•œ ๊ฒƒ์œผ๋กœ, ์–ด๋–ค ํ–‰๋™์— ๋Œ€ํ•œ ๋™์ž‘์„ ์ œ์‹œ

    • ์‹œ๊ฐ์ ์ธ ๋ถ€๋ถ„ ์ˆ˜์ •, ํ–‰์˜ ์„ ํƒ ๊ด€๋ฆฌ, ํ…Œ์ด๋ธ”๋ทฐ์˜ ๊ฐœ๋ณ„ ํ–‰ ํŽธ์ง‘ ๋“ฑ

    • ๋™์ž‘ ์ˆ˜ํ–‰

  • Datasource

    • ๋ฐ์ดํ„ฐ ๋ชจ๋ธ์˜ Delegate

    • ์‹œ๊ฐ์ ์ธ ๋ชจ์–‘์— ๋Œ€ํ•œ ์ •๋ณด ์ œ๊ณต

    • ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„ view๋ฅผ ๊ทธ๋ ค์ฃผ๋Š” ์—ญํ• 

    • ๋ณด์—ฌ์ฃผ๊ธฐ


UITableView๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š”,

UITableViewDatasource, UITableViewDelegate ํ”„๋กœํ† ์ฝœ ์ฑ„ํƒํ•ด์„œ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค.

TableView๋Š” Datasource์™€ Delegate ์ •์˜์— ๋”ฐ๋ผ TableView๋ฅผ ์‚ฌ์šฉ์ž์—๊ฒŒ ์–ด๋–ป๊ฒŒ ํ‘œ์‹œํ•  ๊ฒƒ์ธ์ง€ ๊ฒฐ์ •


  • Datasource: ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„ view ๊ทธ๋ฆฌ๊ธฐ

    • ์ด section์˜ ๊ฐœ์ˆ˜

    • section์˜ ํ–‰ ๊ฐœ์ˆ˜

    • ํ–‰์— ์–ด๋–ค ์ •๋ณด ํ‘œ์‹œ?

  • Delegate: ํ…Œ์ด๋ธ”๋ทฐ์˜ ๋™์ž‘๊ณผ ์™ธ๊ด€ ๋‹ด๋‹น

    • view๊ฐ€ ๋ณ€๊ฒฝ๋˜๋Š” ์‚ฌํ•ญ์„ Delegate๊ฐ€ ๋‹ด๋‹น

    • view๋Š” delegate์—๊ฒŒ ์˜์กดํ•˜์—ฌ ์—…๋ฐ์ดํŠธ

    • ํ–‰์˜ ๋†’์ด

    • ํ–‰์„ ์„ ํƒํ•˜๋ฉด ์–ด๋–ค ์•ก์…˜์„ ํ•  ๊ฒƒ?


UITableViewDataSource

ํ…Œ์ด๋ธ” ๋ทฐ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ˆ˜์ •ํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ์ •๋ณด๋ฅผ ํ…Œ์ด๋ธ” ๋ทฐ ๊ฐ์ฒด์— ์ œ๊ณต

TableView๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ˆ˜์ •ํ•˜๋Š” ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

โœ…ย ๋ฉ”์†Œ๋“œ๋Š” ํ•„์ˆ˜ ์ •์˜ ๋ฉ”์†Œ๋“œ


extension ViewController: UITableViewDataSource {
    //๊ฐ ์„น์…˜์— ํ‘œ์‹œํ•  ํ–‰์˜ ๊ฐœ์ˆ˜ โœ…
		//Intํƒ€์ž…์œผ๋กœ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋ฉด, ํ…Œ์ด๋ธ”๋ทฐ์— ๋ฐ˜ํ™˜๋œ ๊ฐœ์ˆ˜๋งŒํผ ํ–‰์ด ํ‘œ์‹œ๋œ๋‹ค
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {}
    
    //ํŠน์ • ์ธ๋ฑ์Šค row์˜ cell์— ๋Œ€ํ•œ์ •๋ณด๋ฅผ ๋„ฃ์–ด cell ๋ฐ˜ํ™˜ โœ…
		//์…€์„ ๊ตฌ์„ฑํ•˜๊ณ  ๋ณด์—ฌ์ค„ ๋ฐ์ดํ„ฐ๋ฅผ ์„ค์ •ํ•œ ํ›„์—, ์ด ๋ฉ”์†Œ๋“œ์— ๋ฐ˜ํ™˜ํ•˜๋ฉด ๊ตฌ์„ฑํ•œ ์…€์ด ํ…Œ์ด๋ธ”๋ทฐ์— ํ‘œ์‹œ๋œ๋‹ค.
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {}
    
    //์ด ์„น์…˜ ๊ฐœ์ˆ˜
		//Intํƒ€์ž…์œผ๋กœ ์„น์…˜์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ 
    override func numberOfSections(in tableView: UITableView) -> Int {}
    
    //ํŠน์ • ์„น์…˜์˜ ํ—ค๋” ํƒ€์ดํ‹€
    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {}
    
    //ํŠน์ • ์„น์…˜์˜ ํ’‹ํ„ฐ ํƒ€์ดํ‹€
    override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {}
    
    //ํŠน์ • ์œ„์น˜์˜ ํ–‰์ด ํŽธ์ง‘ ๊ฐ€๋Šฅ
		//return true -> ํŽธ์ง‘ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ด์คŒ
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {}
    
    //ํŠน์ • ์œ„์น˜์˜ ํ–‰์„ ์žฌ์ •๋ ฌ ๊ฐ€๋Šฅ
		//return true -> ์žฌ์ •๋ ฌ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ด์คŒ
    override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {}
    
    //ํ…Œ์ด๋ธ” ๋ทฐ ์„น์…˜ ์ธ๋ฑ์Šค ํƒ€์ดํ‹€
    override func sectionIndexTitles(for tableView: UITableView) -> [String]? {}
    
    //์ธ๋ฑ์Šค์— ํ•ด๋‹นํ•˜๋Š” ์„น์…˜ ์•Œ๋ ค์ฃผ๊ธฐ
    override func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {}
    
    //์Šค์™€์ดํ”„ ๋ชจ๋“œ, ํŽธ์ง‘ ๋ชจ๋“œ์—์„œ ๋ฒ„ํŠผ ์„ ํƒํ•˜๋ฉด ํ˜ธ์ถœ
    //ํ–‰์— ๋ณ€๊ฒฝ์‚ฌํ•ญ์„ commit
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {}
    
    //ํ–‰์ด ๋‹ค๋ฅธ ์œ„์น˜๋กœ ์ด๋™๋˜๋ฉด ์–ด๋””์—์„œ ์–ด๋””๋กœ ์ด๋™ํ–ˆ๋Š”์ง€ ์•Œ๋ ค์ฃผ๊ธฐ
    override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {}
}

UITableViewDelegate

ํ…Œ์ด๋ธ” ๋ทฐ์˜ ์‹œ๊ฐ์ ์ธ ๋ถ€๋ถ„์„ ์„ค์ •ํ•˜๊ณ , ํ–‰์˜ ์•ก์…˜ ๊ด€๋ฆฌ, ์•ก์„ธ์„œ๋ฆฌ ๋ทฐ ์ง€์›, ํ…Œ์ด๋ธ” ๋ทฐ์˜ ๊ฐœ๋ณ„ ํ–‰ ํŽธ์ง‘์„ ๋„์›€

ํ•„์ˆ˜ ๋ฉ”์†Œ๋“œ ์—†์Œ


extension ViewController: UITableViewDelegate {
    //ํ–‰ ์„ ํƒ๋˜์—ˆ์„ ๋•Œ ํ˜ธ์ถœ
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {}
    
    //ํ–‰ ์„ ํƒ ํ•ด์ œ๋˜์—ˆ์„ ๋•Œ ํ˜ธ์ถœ
    override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {}
    
    //ํŠน์ • ์œ„์น˜ ํ–‰์˜ ๋†’์ด
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {}
    
    //์ง€์ •๋œ ์„น์…˜์˜ ํ—ค๋”๋ทฐ ๋˜๋Š” ํ‘ธํ„ฐ๋ทฐ์— ํ‘œ์‹œํ•  view๋Š” ์–ด๋–ค๊ฑฐ?
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {}
    override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {}
    
    //์ง€์ •๋œ ์„น์…˜์˜ ํ—ค๋”๋ทฐ ๋˜๋Š” ํ‘ธํ„ฐ๋ทฐ ๋†’์ด
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {}
    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {}
    
    //ํ…Œ์ด๋ธ”๋ทฐ๊ฐ€ ํŽธ์ง‘๋ชจ๋“œ์— ๋“ค์–ด๊ฐ”์„ ๋•Œ ํ˜ธ์ถœ
    override func tableView(_ tableView: UITableView, willBeginEditingRowAt indexPath: IndexPath) {}
    
    //ํ…Œ์ด๋ธ”๋ทฐ๊ฐ€ ํŽธ์ง‘๋ชจ๋“œ์—์„œ ๋‚˜์™”์„ ๋•Œ ํ˜ธ์ถœ
    override func tableView(_ tableView: UITableView, didEndEditingRowAt indexPath: IndexPath?) {}
    
    //ํ…Œ์ด๋ธ”๋ทฐ๊ฐ€ ์…€์„ ์‚ฌ์šฉํ•ด์„œ ํ–‰์„ ๊ทธ๋ฆฌ๊ธฐ ์ง์ „์— ํ˜ธ์ถœ
    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {}
    
    //ํ…Œ์ด๋ธ”๋ทฐ๋กœ๋ถ€ํ„ฐ ์…€์ด ํ™”๋ฉด์— ์‚ฌ๋ผ์ง€๋ฉด ํ˜ธ์ถœ
    override func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {}
}