
์ค๋์ iOS ๊ฐ๋ฐ์์ UITableView๋ฅผ ์ฌ์ฉํด ๊ธฐ๋ณธ์ ์ธ UI๋ฅผ ๊ตฌํํ๊ณ , ๋๋ฏธ ๋ฐ์ดํฐ๋ฅผ ํ์ฉํ์ฌ ํ๋ฉด์ ํ ์คํธํ๋ ๋ฐฉ๋ฒ์ ๋ฐฐ์ ๋ค. ์ด๋ฅผ ํตํด ํ ์ด๋ธ ๋ทฐ์ ๋์์ ๋น ๋ฅด๊ฒ ํ์ธํ ์ ์์๋ค.
UITableView๋ ๋ฆฌ์คํธ ํํ์ ๋ฐ์ดํฐ๋ฅผ ํ๋ฉด์ ํ์ํ ๋ ์ฌ์ฉํ๋ ๊ธฐ๋ณธ ์ปดํฌ๋ํธ์ด๋ค.UITableViewDataSource ํ๋กํ ์ฝ์ ์ฑํํ๊ณ ํ์ ๋ฉ์๋๋ฅผ ๊ตฌํํด์ผ ํ๋ค.numberOfRowsInSection: ์น์
์ ํ ๊ฐ์๋ฅผ ๋ฐํ.cellForRowAt: ๊ฐ ํ์ ํ์ํ ์
์ ๋ฐํ.top, leading, trailing, bottom ์ ์ฝ ์กฐ๊ฑด์ ์ถ๊ฐํ๋ค.tableView.snp.makeConstraints { make in
make.top.equalTo(titleLabel.snp.bottom).offset(16)
make.leading.trailing.bottom.equalToSuperview()
}
private let dummyData = ["ํผ์นด์ธ", "๊ผฌ๋ถ๊ธฐ", "ํ์ด๋ฆฌ", "์ด์ํด์จ", "๋ผ์ด์ธ"]
UITableViewDataSource์ cellForRowAt ๋ฉ์๋์์ ์ฌ์ฉํ์ฌ ๊ฐ ์
์ ํ์ํ๋ค.func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
cell.textLabel?.text = dummyData[indexPath.row]
return cell
}
dataSource๋ฅผ ์ค์ ํด์ผ ํ๋ค.viewDidLoad์์ tableView.dataSource = self๋ฅผ ์ถ๊ฐํ์ฌ ์ฐ๊ฒฐํ๋ค.override func viewDidLoad() {
super.viewDidLoad()
setupUI()
tableView.dataSource = self
}
private let dummyData = ["ํผ์นด์ธ", "๊ผฌ๋ถ๊ธฐ", "ํ์ด๋ฆฌ", "์ด์ํด์จ", "๋ผ์ด์ธ"]
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dummyData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
cell.textLabel?.text = dummyData[indexPath.row]
return cell
}
}
ํ ์ด๋ธ ๋ทฐ๋ iOS ์ฑ ๊ฐ๋ฐ์์ ๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ด๋ฉด์๋ ์ค์ํ ์ปดํฌ๋ํธ์ด๋ค. ํนํ, ๋๋ฏธ ๋ฐ์ดํฐ๋ฅผ ํ์ฉํ๋ฉด UI๋ฅผ ๋น ๋ฅด๊ฒ ํ์ธํ ์ ์์ด ๊ฐ๋ฐ ํจ์จ์ด ๋์์ง๋ค. ์์ผ๋ก ๋ ๋ณต์กํ ๋ฐ์ดํฐ ๊ตฌ์กฐ์ ์ปค์คํ ์ ์ ์ฐ์ตํด ๋๊ฐ ๊ฒ์ด๋ค.