์ฒซ๋ฒ์งธ ์ฑ ๊ฐ๋ฐ ์ค์ต์ผ๋ก setting app์ cloneํ๋ ๊ฒ์ ํด๋ณด์๋ค.
์ฌ๊ธฐ์ ์ฃผ๋ก ๋ฐฐ์ฐ๋ ๋ด์ฉ์ table view์ ์ฌ์ฉ์ด๋ค.
๋จผ์ ํด๋ก ํ iphone setting app๊ณผ ์ค๋๊น์ง ์์ฑํ ํด๋ก ์ฑ์ ๋ชจ์ต์ด๋ค.
(์์ง ์ข ๋ง์ด ๋ค๋ฆ)
๋จผ์ ๋ฉ์ธ ์คํ ๋ฆฌ๋ณด๋์ ๋ชจ์ต์ ๋ณด๋ฉด
์ด๋ ๊ฒ ์๊ฒผ๋ค. ์ ๋ ๊ฒ table view๋ฅผ ํ ํ๋ฉด ๊ฐ๋์ฐจ๊ฒ ๋จผ์ ์์ฑ์ ํด ๋๊ณ ,
๊ทธ ์์ table view cell ์ด๋ผ๊ณ ํ๋ ์์๋ฅผ ๋ฃ๋๊ฑฐ๋ค.
์ ์์์ ์ง์ cell๋ค์ ์์ฑํด์ ๋ฃ์ ์๋ ์์ง๋ง,
๊ทธ๋ผ ํต์ผ์ฑ๋ ๋จ์ด์ง๊ณ ๋๊ฐ์ ๊ตฌ์กฐ์ cell๋ค์ ์ฌ์ฉํ๋ค๋ฉด ๋ฏธ๋ฆฌ ํ์ ๋ง๋ค์ด ๋๋ ๊ฒ์ด ๋ซ๊ธฐ ๋๋ฌธ์
MenuCell, ProfileCell์ ์ด๋ฆ์ผ๋ก ๋๊ฐ์ง ๊ตฌ์กฐ์ cell์ ๋ง๋ ๋ค.
setting app ์๋ณธ์ ๊ตฌ์กฐ๋ฅผ ๋ณด๋ฉด ๋งจ ์ฒ์์ ๋์์๋ cell์ ํ๋กํ ์ฌ์ง์ด ์๋ ์ด๋ฏธ์ง๋ทฐ ํ๋์ ํ ์คํธ ๋ ์ด๋ธ ๋๊ฐ๊ฐ ์๋ ๊ฒ์ ๋ณผ ์ ์๊ณ ๊ทธ ์๋๋ก ๋งค๋ด๋ค์ ์ ๋ถ๋ค ์์ด์ฝ ์ด๋ฏธ์ง, ๋ ์ด๋ธ, ํ์ดํ ์์ด์ฝ ์ด๋ ๊ฒ ์ธ๊ฐ์ง์ ๊ตฌ์ฑ์์๋ก ๋์ด์๋ค.
๊ทธ๋์ ๊ทธ ๊ตฌ์กฐ๋ฅผ ๋๊ฐ์ด ๋ง์ถฐ์ ๋๊ฐ์ง cell๊ณผ ๊ฐ cell view์ ๋ํ controller.swift๋ฅผ ์์ฑํ๋ค.
ProfileCell.swift ๋ MenuCell.swift ์์ ์ฝ๋๋ค์ ๋ฑํ ๋ณผ ํ์ ์๊ณ ,
์ค์ํ๊ฑด ๋ฉ์ธ ์คํ ๋ฆฌ๋ณด๋์ ์ฐ๊ฒฐ๋ ViewController.swift์ด๋ค.
import UIKit
class ViewController: UIViewController{
@IBOutlet weak var settingTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
settingTableView.delegate = self
settingTableView.dataSource = self
settingTableView.register(UINib(nibName: "ProfileCell", bundle: nil), forCellReuseIdentifier: "ProfileCell")
settingTableView.register(UINib(nibName: "MenuCell", bundle: nil), forCellReuseIdentifier: "MenuCell")
}
}
๋จผ์ ์ฒซ๋ฒ์งธ ๋ธ๋ก๋ถํฐ ๋ณด๋ฉด
์ผ๋จ settingTableView๋ผ๋ ์ด๋ฆ์ผ๋ก ํ ์ด๋ธ๋ทฐ๋ฅผ ๊ฐ์ ธ์ค๊ณ
viewDidLoadํจ์์์ ์๊น ๋ฐ๋ก ๋ฐ๋ก ๋ง๋ค์๋ ProfileCell, MenuCell์ settingTableView์ ๋ฑ๋กํ๋ค.
์ฌ๊ธฐ์, ์ด๋ ๊ฒ ๋ฑ๋กํด์ ์ฌ์ฉํ๋ ค๋ฉด ์ด ํ ์ด๋ธ๋ทฐ๊ฐ UITableViewDelegate, UITableViewDataSource ์ด ๋๊ฐ์ง์ ํ๋กํ ์ฝ์
๋ฐ๋ผ์ผ ํ๋ฏ๋ก ๊ด๋ จ ์ฝ๋๋ ์์ฑํด์ฃผ๊ณ , ํ๋กํ ํจ ๊ตฌํ๋ถ๋ ๋ฐ์ ์์ฑํ๋ค.
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "ProfileCell", for: indexPath)
return cell
}
let cell = tableView.dequeueReusableCell(withIdentifier: "MenuCell", for: indexPath)
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == 0 {
return UITableView.automaticDimension
}
return 60
}
}
์ด 3๊ฐ์ง์ ๊ตฌํ๋ผ์ผํ ํจ์๊ฐ ์๋ค.
1.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
์ด ๋ช๊ฐ์ ํ ์ด๋ธ๋ทฐ cell์ ๋ณด์ฌ์ค๊ฑด์ง ์ ํ๋ ๋ถ๋ถ์ด๋ค. ๋๋ ์ด 5๊ฐ๋ก ํ๋ค.
2.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "ProfileCell", for: indexPath)
return cell
}
let cell = tableView.dequeueReusableCell(withIdentifier: "MenuCell", for: indexPath)
return cell
}
๊ทธ 5๊ฐ์ ํ ์ด๋ธ๋ทฐ cell์ ์ด๋ค ๋ชจ์์ cell์ ๋ณด์ฌ์ค ๊ฑด์ง ์ ํ๋ ๋ถ๋ถ์ด๋ค.
์ฌ๊ธฐ์ ์กฐ๊ฑด๋ฌธ์ผ๋ก index 0๋ฒ (์ฒซ๋ฒ์งธ) cell์๋ profile cell์, ๋๋จธ์ง ๋ถ๋ถ์๋ menu cell์ ๋ณด์ฌ์ฃผ๋๋ก ๋ง๋ ๋ค.
3.
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == 0 {
return UITableView.automaticDimension
}
return 60
}
์ฌ๊ธฐ์๋ ํ ์ด๋ธ๋ทฐ์ ์ฌ์ด์ฆ๋ฅผ ์์๋ก ์ ํ๋ ๋ถ๋ถ์ด๋ค.
๋ญ ๊ตณ์ด ์ํด์ค๋ ๋๋๋ฐ ์ผ๊ด์ฑ์ ์ํด
์ฒซ๋ฒ์งธ cell (Profile cell)์ ์คํ ๋ฆฌ๋ณด๋์์ ์ค์ ํ๋๋ก ์๋์ ์ผ๋ก ์ค์ ๋๊ฒ ํ๊ณ
๋๋จธ์ง๋ ๋์ด 60์ผ๋ก ๊ณ ์ ํด๋จ๋ค.
๊ทธ๋ ๊ฒ ํด์ ์์ฑ๋ ๋ชจ์ต์ด ์ด๊ฑฐ๋ค.
+) ์ฌ๊ธฐ์ ProfileCell.swift์ UI์์๋ค์ ํธ์ง์ ์ฝ๋๋ก ํ ๋ถ๋ถ์ด ์๋ค.
import UIKit
class ProfileCell: UITableViewCell {
@IBOutlet weak var topTitle: UILabel!
@IBOutlet weak var bottomDescription: UILabel!
@IBOutlet weak var profileImageView: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
let profileHeight : CGFloat = 55
profileImageView.layer.cornerRadius = profileHeight / 2
topTitle.textColor = .blue
topTitle.font = UIFont.systemFont(ofSize: 20)
bottomDescription.textColor = .darkGray
bottomDescription.font = UIFont.systemFont(ofSize: 16)
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
์ด๋ฏธ์ง๋ทฐ, ๋๊ฐ์ง์ ๋ ์ด๋ธ์ ๊ฐ๊ฐ ๊ฐ์ ธ์์
๋จผ์ profileImageView๋ layer.cornerRadius๋ฅผ ์ฌ์ฉํ์ฌ height์ ๋ฐ ํฌ๊ธฐ๋ก ๋์ ์๋ผ ์์ด๋๊ฒ ๋ง๋ค์๊ณ ,
๋๋จธ์ง ๋ ์ด๋ธ๋ค์ ํฐํธ ํฌ๊ธฐ์ ์์ ๋ฐ๊ฟ ์ฃผ๋ ์์ ์ ํ๋ค.
์ดํ์๋ ๊ฐ๊ฐ ๋ทฐ์ ๋ ์ด๋ธ์ ๋ฐ์ดํฐ๋ฅผ ๋ฃ์ด์ ๋ ๋ค๋ฌ์ด์ง ํด๋ก ์ฑ์ ๋ง๋ค๊ณ ์ ํ๋ค.
'๐ก work work work > swift' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
swift ์ค์ต - 4 : OnBoardingView (0) | 2022.02.21 |
---|---|
swift ์ค์ต - 3.2 : Setting App Clone (0) | 2022.02.15 |
swift ์ค์ต - 2 : Dispatch Queue (0) | 2022.02.14 |
swift ์ค์ต - 1 : Passing Data (0) | 2022.02.09 |
swift ๊ณต๋ถ - 7 : Extension, Protocol, Generic, ๊ณ ์ฐจํจ์ (0) | 2022.02.08 |
๋๊ธ