๊ธ€ ์ž‘์„ฑ์ž: ๊ฐœ๋ฐœํ•˜๋Š” ํ›ˆ์ด

dequeueReusableCell์˜ ์ •์ฒด

๋งŒ์•ฝ ํ…Œ์ด๋ธ” ๋ทฐ๋ฅผ ํ†ตํ•ด ๋ณด์—ฌ์ค„ ๋ฐ์ดํ„ฐ๊ฐ€ ์ˆ˜๋งŒ ๊ฐœ๋ผ๋ฉด ํ…Œ์ด๋ธ” ๋ทฐ๋ฅผ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ํšจ๊ณผ์ ์ผ๊นŒ์š”? ๋ฏธ๋ฆฌ ๋ชจ๋“  ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์…€์„ ๋งŒ๋“ค์–ด๋‘๋ฉด ๋ชจ๋ฐ”์ผ ํœด๋Œ€ํฐ์˜ ๋ฉ”๋ชจ๋ฆฌ๋Š” ๊ฐ€๋“ ์ฐฐ์ง€๋„ ๋ชจ๋ฆ…๋‹ˆ๋‹ค. ๋กœ๋“œํ•˜๊ธฐ ์œ„ํ•ด ์‹œ๊ฐ„๋„ ๋งŽ์ด ์‚ฌ์šฉํ•ด์•ผ ํ•  ๊ฒƒ์ด๊ณ ์š”. ์•„๋งˆ๋„ ๊ฐ€์žฅ ํšจ๊ณผ์ ์ธ ๋ฐฉ๋ฒ•์€ ํ˜„์žฌ ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ณด์ด๋Š” ์ •๋ณด๋งŒ์„ ๋กœ๋“œํ•˜๋Š” ๊ฒƒ์ด๊ฒ ์ฃ . ์•„์ง ํ™”๋ฉด์— ๋‚˜ํƒ€๋‚˜์ง€ ์•Š์€ ๋ฐ์ดํ„ฐ๋Š” ์‚ฌ์šฉ์ž์—๊ฒŒ๋Š” ํ•„์š” ์—†๋Š” ๋ฐ์ดํ„ฐ์ด๋‹ˆ๊นŒ์š”.

 

๋จผ์ € ์• ํ”Œ์˜ ๊ณต์‹๋ฌธ์„œ๋ถ€ํ„ฐ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

๐Ÿ’กFor performance reasons, a table viewโ€™s data source should generally reuse UITableViewCell objects when it assigns cells to rows in its tableView(_:cellForRowAt:) method. A table view maintains a queue or list of UITableViewCell objects that the data source has marked for reuse. Call this method from your data source object when asked to provide a new cell for the table view. This method dequeues an existing cell if one is available or creates a new one using the class or nib file you previously registered. If no cell is available for reuse and you did not register a class or nib file, this method returns nil.

 

์„ฑ๋Šฅ์ƒ์˜ ์ด์œ ๋กœ ํ…Œ์ด๋ธ” ๋ทฐ๋Š” ์…€์„ ์žฌ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๋งŒ์•ฝ ์ด๋ฏธ ์žฌ์‚ฌ์šฉํ•˜๊ธฐ๋กœ ๋“ฑ๋ก๋œ ์…€์— ๋Œ€ํ•œ nib๋‚˜ ํด๋ž˜์Šค๊ฐ€ ์žˆ๋‹ค๋ฉด ์žฌ์‚ฌ์šฉํ•˜๊ณ , ์—†๋‹ค๋ฉด nil์„ ๋ฐ˜ํ™˜ํ•˜๋„ค์š”. ์žฌ์‚ฌ์šฉํ•˜๊ธฐ๋กœ ๋“ฑ๋ก๋œ ์…€์€ ์šฐ๋ฆฌ๊ฐ€ ์Šคํ† ๋ฆฌ๋ณด๋“œ์—์„œ identifier๋ฅผ ๋“ฑ๋กํ•œ ์…€์„ ๋งํ•˜๋Š”๊ฑฐ๊ฒ ์ฃ ! ๋“ฑ๋ก๋œ ํด๋ž˜์Šค๋‚˜ nib๊ฐ€ ์žˆ์ง€๋งŒ ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์…€์ด ์—†๋‹ค๋ฉด ํ•ด๋‹น ํด๋ž˜์Šค๋‚˜ nib๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค๊ณ , ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์…€์ด ์žˆ๋‹ค๋ฉด ํ•ด๋‹น ์…€์„ ์žฌ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค!

 

๐Ÿ’กIf you registered a class for the specified identifier and a new cell must be created, this method initializes the cell by calling its init(style:reuseIdentifier:) method. For nib-based cells, this method loads the cell object from the provided nib file. If an existing cell was available for reuse, this method calls the cellโ€™s prepareForReuse()
 method instead.

 

๊ทธ๋ฆฌ๊ณ  ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์…€์ด ์žˆ์œผ๋ฉด ์ด ๋ฉ”์„œ๋“œ๊ฐ€ ์ž๋™์œผ๋กœ ์…€์˜ prepareForReuse() ๋ฅผ ๋ถ€๋ฅธ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฉ”์„œ๋“œ๋Š” ์…€์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์†์„ฑ ๊ฐ’๋“ค์„ ์ดˆ๊ธฐํ™”ํ•ด์ฃผ๋Š” ๋ฉ”์„œ๋“œ์˜ˆ์š”! ์…€์ด ๋‹ค๋ฅธ ๋‚ด์šฉ์˜ ์…€๋กœ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋Š” ๋ฉ”์„œ๋“œ๊ฒ ์ฃ ?

 

๊ทธ๋ž˜์„œ ํ…Œ์ด๋ธ” ๋ทฐ๋Š” ํ˜„์žฌ ํ™”๋ฉด์— ๋ณด์—ฌ์ง€๋Š” ์ธ๋ฑ์Šค์— ๋Œ€ํ•œ ์…€ ๋งŒ์„ ์š”์ฒญํ•˜๊ณ , ์‹ฌ์ง€์–ด๋Š” ํ™”๋ฉด ๋ฐ–์œผ๋กœ ๋‚˜๊ฐ”๋˜ ์…€์„ ๋‹ค์‹œ ์žฌ์‚ฌ์šฉํ•˜๊ธฐ๊นŒ์ง€ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์…€์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์กฐ๊ธˆ๋งŒ ๋งŒ๋“ค์–ด๋‘๊ณ ๋„ ๊ฐ™์€ ์ธ์Šคํ„ด์Šค๋ฅผ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด ๊ณ„์† ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋‹ˆ๊นŒ์š”. 

์ด๋ ‡๊ฒŒ ํ…Œ์ด๋ธ”์ด ์žˆ๋‹ค๊ณ  ํ•ด๋ณผ๊ฒŒ์š”. ์ด ํ…Œ์ด๋ธ” ๋ทฐ์—์„œ ๋ณด์—ฌ์ฃผ๊ณ ์žํ•˜๋Š” ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜๋Š” 10๊ฐœ๋ผ๊ณ  ๊ฐ€์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ํ™”๋ฉด์ด ์‹œ์ž‘๋˜๊ณ  ํ…Œ์ด๋ธ” ๋ทฐ์—๋Š” ์ดˆ๊ธฐ์— ์„ค์ •ํ–ˆ๋˜ ๊ฐœ์ˆ˜๋งŒํผ์˜ ์…€์ด ๋ณด์ด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์…€๋“ค์€ "cell"์ด๋ผ๋Š” reuse identifier๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์ฃ . ์žฌ์‚ฌ์šฉํ•˜๊ธฐ๋กœ ๋“ฑ๋ก์€ ๋˜์–ด์žˆ์ง€๋งŒ ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ธ์Šคํ„ด์Šค๊ฐ€ ์—†์œผ๋‹ˆ dequeueResusableCell ๋ฉ”์„œ๋“œ๋Š” ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค๋ฅผ ํ–‰์˜ ๊ฐœ์ˆ˜๋งŒํผ ์ƒ์„ฑํ•  ๊ฒƒ ์ž…๋‹ˆ๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  ์ด์ œ ์‚ฌ์šฉ์ž๊ฐ€ ํ…Œ์ด๋ธ” ๋ทฐ๋ฅผ ์œ„๋กœ ์Šค์™€์ดํ”„ ํ•ฉ๋‹ˆ๋‹ค.

"1"์ด๋ผ๋Š” ํ…์ŠคํŠธ๋ฅผ ์ฝ˜ํ…์ธ ๋กœ ๋‹ด๊ณ  ์žˆ๋˜ ์…€์ด ํ™”๋ฉด ๋ฐ–์œผ๋กœ ์‚ฌ๋ผ์ง€๊ณ  ์ด ์…€์€ ๊ณง๋ฐ”๋กœ Cell์ด๋ผ๋Š” Identifier๋ฅผ ์‚ฌ์ง„ ์…€๋“ค์„ ๋ชจ์•„๋‘๋Š” ํ์— ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค. 

 

ํ…Œ์ด๋ธ” ๋ทฐ๋Š” ์ž์‹ ์ด ๋ณด์—ฌ์ฃผ์–ด์•ผ ํ•˜๋Š” ํ–‰์˜ ๊ฐœ์ˆ˜๋ฅผ ์•Œ๊ณ  ์žˆ์œผ๋‹ˆ ๋‹ค์Œ ํ–‰์˜ ๋Œ€ํ•œ ์ •๋ณด๋ฅผ Data Source์—๊ฒŒ ์š”์ฒญํ•˜๊ฒ ์ฃ ? ์ด๋•Œ ์šฐ๋ฆฌ๊ฐ€ ์œ„์—์„œ ๊ตฌํ˜„ํ•œ cellForRowAt ๋ฉ”์„œ๋“œ๊ฐ€ ๋ถˆ๋ฆฌ๋ฉด์„œ ํ์— ์žˆ๋˜ ์…€์„ dequeue ํ•ฉ๋‹ˆ๋‹ค.

 

๋งˆ์ง€๋ง‰์œผ๋กœ prepareForReuse๋ฅผ ํ˜ธ์ถœํ•ด์„œ ์…€์„ ์ดˆ๊ธฐํ™”ํ•ด์ฃผ๊ณ , cellForRowAt์— ๊ตฌํ˜„๋œ ์ฝ”๋“œ์— ๋”ฐ๋ผ ๋‚ด์šฉ์„ ๋ณ€๊ฒฝํ•œ ๋’ค ์ถ”๊ฐ€๋œ index path์— ๋Œ€ํ•œ ์…€๋กœ ๋ฐ˜ํ™˜ํ•˜๋ฉด ํ…Œ์ด๋ธ” ๋ทฐ๋Š” ์ด ์…€์„ ํ™”๋ฉด์— ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.

 

 

์‚ฌ์šฉ์ž๊ฐ€ ํ…Œ์ด๋ธ”์„ ์กฐ์ž‘ํ•ด์„œ ๋ณด์ผ ๋‚ด์šฉ์ด ๋ฐ”๋€” ๋•Œ๋งˆ๋‹ค cellForRowAt ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜๊ณ  dequeueReusableCell๋„ ํ•จ๊ป˜ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค. ์ด์ œ ํ…Œ์ด๋ธ” ๋ทฐ๊ฐ€ ์…€์„ ์–ด๋–ป๊ฒŒ ๋ณด์—ฌ์ฃผ๋Š”์ง€ ์žฌ์‚ฌ์šฉ์˜ ์›๋ฆฌ๋ฅผ ์ดํ•ดํ•˜์…จ๋‚˜์š”? ๋ชจ๋ฐ”์ผ ํ™˜๊ฒฝ์ด๋ผ์„œ ๊ทธ๋Ÿฐ์ง€ ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•ด ์ตœ์ ํ™”์˜ ๊ทนํ•œ์„ ๋ณด์—ฌ์ฃผ๋Š” ๋ฉ”์ปค๋‹ˆ์ฆ˜์ธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์• ํ”Œ ๊ฐœ๋ฐœ์ž๋“ค ์ง„์งœ ์ฉ”์–ด..

 

๋‹ค์Œ ํฌ์ŠคํŠธ์—์„œ๋Š” ์ปค์Šคํ…€ ์…€์„ ๋งŒ๋“ค์–ด์„œ ์—ฐ๋ฝ์ฒ˜ ์•ฑ์˜ ์ตœ๊ทผ ํ†ตํ™” ๋ชฉ๋ก์„ ๋งŒ๋“œ๋Š” ๊ณผ์ •์„ ์ •๋ฆฌํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค! ์ฝ์–ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!!

๋Œ“๊ธ€

๋Œ“๊ธ€์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.