๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŽ iOS/UIKit

[iOS/Swift] TableView ๋ฅผ ์œ„ํ•œ Protocol :: Datasource, Delegate

by Danna 2021. 3. 1.
728x90
728x90

TableView ๋ฅผ ์œ„ํ•œ Protocol

UITableView ๋ฅผ ์œ„ํ•œ Protocol ์€ UITableViewDataSource, UITableViewDelegate ๊ฐ€ ์žˆ๋‹ค. ์—ฌ๊ธฐ์„œ Protocol ์€ TableView๋ฅผ ์ด์šฉํ•˜๊ธฐ ์œ„ํ•ด ํ•ด์•ผํ•  ์ผ๋“ค์˜ ๋ชฉ๋ก, ์ฆ‰ ๊ตฌํ˜„ํ•ด์•ผํ•  method ๋“ค์ด๋‹ค. 

 

protocol ์ค€์ˆ˜๋Š” ํด๋ž˜์Šค๊ฐ„ ์ƒ์†์„ ํ•˜๋“ฏ์ด ์ž‘์„ฑํ•ด์ฃผ๋ฉด ๋œ๋‹ค. UITableViewDataSource protocol ์˜ ๊ฒฝ์šฐ, ํ•„์ˆ˜ ๊ตฌํ˜„ ํ•จ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•˜์ง€ ์•Š์œผ๋ฉด, ์•„๋ž˜์™€ ๊ฐ™์€ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. Fix ๋ฅผ ๋ˆ„๋ฅด๋ฉด ์ž๋™์œผ๋กœ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค.

extension BountyViewController: UITableViewDataSource {
	
}

 

UITableViewDataSource

protocol UITableViewDataSource
๐Ÿ’ก The methods adopted by the object you use to manage data and provide cells for a table view.

Table view data source object ๋Š” ํ•ด๋‹น ํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒํ•œ๋‹ค. Table view ๋Š” ๋ฐ์ดํ„ฐ ๊ธฐ๋ฐ˜์ด๊ณ , ์ผ๋ฐ˜์ ์œผ๋กœ ์ œ๊ณต๋œ data source object ๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์–ป๋Š”๋‹ค. data source object ๋Š” ์•ฑ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€๋ฆฌํ•˜๊ณ , table ์˜ cell ์„ ์ƒ์„ฑํ•˜๊ณ  ๊ด€๋ฆฌํ•  ์ฑ…์ž„์„ ๊ฐ–๋Š”๋‹ค. 

 

Data source object ์˜ ์ฑ…์ž„์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. 

  • table ์˜ section ๊ณผ row ์˜ ๊ฐœ์ˆ˜๋ฅผ ์•Œ๋ ค์ฃผ๋Š” ๊ฒƒ
  • table ์˜ ๊ฐ row ์— cell ์„ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ
  • section header ์™€ footer ์— title ์„ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ
  • table ์˜ index ๋ฅผ ์„ค์ •ํ•˜๋Š” ๊ฒƒ
  • ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€๊ฒฝํ•ด์•ผ ํ•˜๋Š” user ๋˜๋Š” table ์ดˆ๊ธฐ ์—…๋ฐ์ดํŠธ์— ์‘๋‹ตํ•˜๋Š” ๊ฒƒ

TableView ๋ฅผ ์ด์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ ํ•„์ˆ˜๋กœ ๊ตฌํ˜„ํ•ด์•ผ ํ•˜๋Š” method ๊ฐ€ ์žˆ๋‹ค. TableView ์˜ cell ์ด ๋ช‡๊ฐœ์ธ์ง€? Table view ๋ฅผ ์–ด๋–ป๊ฒŒ ๋ณด์—ฌ์ค„ ๊ฒƒ์ธ์ง€? ์— ๋Œ€ํ•œ ์งˆ๋ฌธ์ด๋‹ค. ํ•ด๋‹น ์งˆ๋ฌธ์€ ๋‹ค์Œ method ๋ฅผ ๊ตฌํ˜„ํ•จ์œผ๋กœ ๋‹ต๋ณ€ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

1. tableView(_:numberOfRowsInSection:)

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
  • ํŠน์ • section ์— ํ‘œ์‹œํ•  row ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฌป๋Š” method
  • return ๊ฐ’์œผ๋กœ ๋ช‡ ๊ฐœ์˜ ์…€์„ ๋ณด์—ฌ์ค„์ง€์— ๋Œ€ํ•œ ๋‹ต๋ณ€์„ ๊ตฌํ˜„ํ•˜๋ฉด ๋œ๋‹ค. 
  • section ์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ์…€์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. 

2. tableView(_:cellForRowAt:)

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
  • ํŠน์ • ์œ„์น˜์— ํ‘œ์‹œํ•  cell ์„ ์š”์ฒญํ•˜๋Š” method
  • cell ์„ ์ƒ์„ฑํ•  ๋•Œ, custom cell ์„ ์žฌ์‚ฌ์šฉํ•˜๋ ค๋ฉด dequeueReusableCell(withIdentifier:for:) method ๋ฅผ ์ด์šฉํ•œ๋‹ค.
  • cell ์„ ๋งŒ๋“  ํ›„์—, data ๊ฐ’๋“ค์„ ํ†ตํ•ด cell ์˜ ์†์„ฑ๋“ค์„ ์—…๋ฐ์ดํŠธํ•  ์ˆ˜ ์žˆ๋‹ค.
  • return ๊ฐ’์œผ๋กœ UITableViewCell ์„ ์ƒ์†๋ฐ›์€ object ๋ฅผ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•œ๋‹ค. (nil ๋ฐ˜ํ™˜์‹œ ์—๋Ÿฌ ๋ฐœ์ƒ)

 

UITableViewDelegate

protocol UITableViewDelegate
๐Ÿ’กMethods for managing selections, configuring section headers and footers, deleting and reordering cells, and performing other actions in a table view.

Table view delegate object๋Š” ํ•ด๋‹น ํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒํ•œ๋‹ค. Table view ๋‚ด์— ํด๋ฆญ๋œ ์…€์„ ๋งค๋‹ˆ์ง•ํ•˜๊ฑฐ๋‚˜, ์„น์…˜์˜ header์™€ footer ์„ค์ •, ์…€์„ ์‚ญ์ œํ•˜๊ฑฐ๋‚˜ ์žฌ์ก๋Ÿดํ•˜๊ธฐ ์œ„ํ•ด ์ด์šฉํ•œ๋‹ค.

 

ํ•ด๋‹น ํ”„๋กœํ† ์ฝœ์˜ method ๋“ค์€ ๋‹ค์Œ ๊ธฐ๋Šฅ๋“ค์„ ์ œ๊ณตํ•œ๋‹ค.

  • custom header ์™€ footer view ๋ฅผ ๋งŒ๋“ค๊ณ  ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ
  • row, header, footer ์˜ ๋†’์ด๋ฅผ ์ง€์ •ํ•˜๋Š” ๊ฒƒ
  • ๋” ๋‚˜์€ ์Šคํฌ๋กค ์ง€์›์„ ์œ„ํ•ด ๋†’์ด์˜ ์ถ”์ •์น˜๋ฅผ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ
  • row ์˜ ๋“ค์—ฌ์“ฐ๊ธฐ ์ˆ˜์ค€์„ ์ง€์ •ํ•˜๋Š” ๊ฒƒ
  • ์„ ํƒ๋œ row ์— ๋Œ€ํ•ด ์‘๋‹ตํ•˜๋Š” ๊ฒƒ
  • table row ๋ฅผ ์Šค์™€์ดํ”„ํ•˜๋Š” ๋“ฑ ์•ก์…˜์— ์‘๋‹ตํ•˜๋Š” ๊ฒƒ
  • table content ๋ฅผ ํŽธ์ง‘ํ•  ์ˆ˜ ์žˆ๋„๋ก ์ง€์›ํ•˜๋Š” ๊ฒƒ

UITableViewDelegate ํ”„๋กœํ† ์ฝœ์€ ํ•„์ˆ˜ ๊ตฌํ˜„ method ๊ฐ€ ์—†๋‹ค. ๋‹ค์Œ์€ ์ฃผ๋กœ ์ž‘์„ฑํ–ˆ๋˜ method ์ด๋‹ค.

 

tableView(_didSelectRowAt:) 

optional func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)

 

  • delegate (๋Œ€๋ฆฌ์ธ)์—๊ฒŒ row๊ฐ€ ์„ ํƒ๋˜์—ˆ์Œ์„ ์•Œ๋ ค์ฃผ๋Š” ํ•จ์ˆ˜. 
  • ์…€์„ ์„ ํƒํ–ˆ์„ ๋•Œ ์–ด๋–ป๊ฒŒ ํ• ์ง€์— ๋Œ€ํ•ด ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜์ด๋‹ค.
  • ์˜ˆ์‹œ๋กœ, TableView ๊ฐ€ List page ์ด๊ณ  Detail page ๊ฐ€ ์žˆ๋‹ค๋ฉด ํ•ด๋‹น method ์—์„œ performSegue ๋ฅผ ํ˜ธ์ถœํ•ด Detail page ๋กœ ๋„˜์–ด๊ฐ€๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.
extension ListViewController: UITableViewDelegate {

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("--> \(indexPath.row)")
        performSegue(withIdentifier: "showDetail", sender: indexPath.row)
    }
}

๊ณต์‹ ๋ฌธ์„œ

์ฐธ๊ณ  ๋ฌธ์„œ

728x90
728x90