<Table View ๋ฆฌ์คํธ ๋ง๋ค๊ธฐ 2๋ฒ์งธ ๋ฐฉ๋ฒ>
2024.03.02 - [Study/YouTube ๊ฐ์ ๊ณต๋ถ] - ํ
์ด๋ธ ๋ทฐ(UITableView)_[iOS/Swift]_(1)
์ฌ๊ธฐ์ ๊ทธ๋๋ก ์งํ!
โ Main.storyboard - Table view์ Table View Cell ์ถ๊ฐ - ์ฌ์ด์ฆ ์กฐ๊ธ ๋๋ฆฌ๊ณ , ์์ Label ์ถ๊ฐ
(Label → ์ค์์ ๋ ฌ, Add New Constraints์์ ์ํ์ข์ฐ 0์ผ๋ก ์
ํ
)
โก ์ถ๊ฐํ Table View cell ํด๋ฆญ - ์ฐ์ธก ์ฐฝ → Identifier : ์ด๋ฆ
('Identifier: Type1' ์ผ๋ก ํ๊ฒ ์, <์์ ๋กญ๊ฒ>)
โข ์์ 1๋ฒ์งธ ๋ฐฉ๋ฒ์์ reuseIdentifier์ "TableViewFirstType"์ด๋ผ๊ณ ์ ํ์๋ค.
์ด์ ๋ ์ด ์ด๋ฆ์ด "Type1"์ด ๋๋ค.
์ด "Type1"์ ์ง์ ํ๋ ํด๋์ค๊ฐ ํ์ํ๋ค. ์ด๊ฑธ ์ด๋ป๊ฒ ๋ง๋๋๋!
New file - Swift file - ์ด๋ฆ ์ ํ๊ธฐ
(์์๋ณด๊ธฐ ์ฝ๊ฒ "Type1"์ผ๋ก ํ๊ฒ ์. <์์ ๋กญ๊ฒ>)
์ฌ๊ธฐ์ import foundation → import UIKit
์์ ๊ฐ์ด
class Type1: UITableViewCell { }
์ด๋ ๊ฒ ํ๋ฉด Type1์ด UITableViewCell์ ์์๋ฐ๊ฒ ๋๋ค.
์ด ํด๋์ค๋ ์๊น Main.storyborad์์ ๋ง๋ ์
๊ณผ ์ฐ๊ฒฐํ ์ ์๊ฒ ํ๋ ์ญํ ์ ํ๋ค.
โฃ Main.storyboard - Type1 - ์ฐ์ธก ์ฐฝ → class: Type1
์ด๋ ๊ฒ ํ๊ณ , ๋ฐ์ 'Inherit Module From Target'์ ์ฒดํฌ!
โค ์ด์ ์ค์ ํ๋๋ก ์ ์ฐ๊ฒฐ๋๋์ง ํ์ธํ๊ธฐ!
์์ ํ์ํด ๋์ ๋ถ๋ถ(โ) ํด๋ฆญ!
์ฌ๊ธฐ๋ก ์ด๋๋๋ฉด์ ์ ์ฐ๊ฒฐ๋์์ ํ์ธํ ์ ์๋ค.
์ด์ ์ด ์ํ์์ ์๊น ๋ง๋ Label์ ์ฐ๊ฒฐํด์ผ ํ๋ค.
โฅ Main.storyboard์ Type1.swift ํ๋ฉด์ ๋์์ ๋์๋๊ณ ,
์คํ ๋ฆฌ๋ณด๋์ ์๋ Label์ Type1.swift ์ฝ๋๋ก
drag & drop!!
๋ผ๋ฒจ ์ด๋ฆ ์์ ๋กญ๊ฒ ์ ๊ณ connect!
('Name: LabelText'์ผ๋ก ํ๊ฒ ์. <์์ ๋กญ๊ฒ>)
โฆ ์ด์ ๋ค์ ViewController๋ก Go!
let cell = UITableViewCell.init( ~~~~~~~~) ์ด๊ฑฐ ์ง์ฐ๊ณ ,
let cell = TableViewMain.deqํ๋ฉด
์ด๋ ๊ฒ ์ฌ๋ฌ ๊ฐ์ง๊ฐ ๋ฐ ๊ฒ์ด๋ค.
์ด๊ฑฐ ์ ํ!!
์ฌ๊ธฐ์ dequeueReusableCell์ด ๋ญ๊น? ๐ค
"Reusable, ์ฌ์ฌ์ฉํ ์ ์๋ ์
์ ์ ๋ฆฌํด์ TableViewMain์ ๋๊ธฐ๊ฒ ๋ค."
→ ๊ทธ ์
์ Identifier์ 'Tpye1'์ด์๊ณ , ๊ทธ indexPath ์๋ฒ์ ํด๋ค์๋ ์
์ ๋ง๋๊ฒ ๋๋ฉด,
"Type1"์ด๋ผ๋ ์ ๋ฅผ TableViewMain์๊ฒ ๋ฃ๋๋ค๋ ๋ป.
โง ๋น์นธ ์ฑ์ฐ๊ธฐ.
String → "Type1"
IndexPath → indexPath
โจ ์ด์ , ๊ธฐ์กด์ ๋ฐฉ๋ฒ 1์์ ์ ์๋ cell.textLabel.text = "\(indexPath.row)" ์ง์ฐ๊ณ !!
cell.LabelText ๋ฅผ ์ณ๋ณด๋ฉด??
???? ์๋๋ค. ์์ง??
→ ์์ 'dequeue ~' ๋ ๋ถ๋ชจ ์์ฑ์ด๊ธฐ ๋๋ฌธ์ด๋ค.
์ฌ๊ธฐ์ as ๋ผ๋ ๊ฒ์ ์ฌ์ฉํด ์ฃผ์ด์ผ ํ๋ค.
(as? - ํ์
์ ์์ ํ๊ฒ ์ถ๋ก / as! - ๊ฐ์ ๋ก ๋ณํ)
Type1์ UITableViewCell์ด๋ผ๋ ์์ด์ ์ ์ ์๋ฅผ ๋ฌผ๋ ค๋ฐ์ ์์์ด๋ค. (=์์๋ฐ์)
let cell = TableViewMain.dequeueReusableCell(withIdentifier: "Type1", for: indexPath)
์ด๊ฑด UITableViewCell์ ๋ํ๋ด๊ธฐ ๋๋ฌธ์
"์ ๊ฐ ์๋๋ผ ์์๋ฐ์ Type1์ด์ผ!" ๋ผ๊ณ ์ ์ฒด์ฑ์ ๋ช
ํํ๊ฒ ๋ฐํ์ฃผ์ด์ผ ํ๋ค.
์ด ์ญํ ์ ํ๋ ๊ฒ์ด ๋ฐ๋ก as? ์ as! ์ด๋ค.
(as? - "๋ ๋ง์..?" / as! - "๋ ๋ง์!!" ์ด๋ฐ ๋๋)
์ฆ, ์ฐ๋ฆฌ๊ฐ ์ํ๋ ๊ฑด ์์์ธ 'Type1' ์ด๊ธฐ ๋๋ฌธ์
๋ฐ๋ก ๋ค์ as! Type1 ์ด๋ผ๊ณ ํด์ค์ผ ํจ.
(Type1์ ์ฐ๋ฆฌ๊ฐ ๋ง๋ค์ด์ ํ์คํ๊ธฐ ๋๋ฌธ์ as!)
โฉ ์ด์ cell.LabelText ํ๋ฉด ๋์ด.
→ cell.LabelText.text = "\(indexPath.row)"
๋! ๐ฅน
์คํ Go!!
<๋ฐฉ๋ฒ 1๊ณผ 2์ ์ฐจ์ด์ >
๋ณด๊ธฐ์๋ 1, 2 ์ฐจ์ด๊ฐ ๋ณ๋ก ์์ด ๋ณด์ธ๋ค.
๋์ ๋๋ ์ฐจ์ด๋ผ๋ฉด, 1์ ์ข์ธก ์ ๋ ฌ, 2๋ ์ค์ ์ ๋ ฌ์ด๋ผ๋ ๊ฒ?
๊ทธ๋ฌ๋, ์ค์ ์ 2๋ฒ ๋ฐฉ๋ฒ์ด๋ผ๊ณ ์๊ฐํ๋ฉด ๋๋ค.
1๋ฒ ๋ฐฉ๋ฒ์ ๊ฑฐ์ ์์ฐ๋๊น '์ฐ์ต์ด๋ค..' ์๊ฐํ๋ฉด ๋๋ค.
'๐ฉ๐ปโ๐ป > YouTube ๊ฐ์ ๊ณต๋ถ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ ์ด๋ธ ๋ทฐ(UITableView)_[iOS/Swift]_(1) (0) | 2024.03.02 |
---|---|
์น๋ทฐ(WKWebView)_[iOS/Swift] (0) | 2024.03.01 |