๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ก work work work/swift

swift ์‹ค์Šต - 3.1 : Setting App Clone

by hanwitjus 2022. 2. 14.

์ฒซ๋ฒˆ์งธ ์•ฑ ๊ฐœ๋ฐœ ์‹ค์Šต์œผ๋กœ setting app์„ cloneํ•˜๋Š” ๊ฒƒ์„ ํ•ด๋ณด์•˜๋‹ค.

 

์—ฌ๊ธฐ์„œ ์ฃผ๋กœ ๋ฐฐ์šฐ๋Š” ๋‚ด์šฉ์€ table view์˜ ์‚ฌ์šฉ์ด๋‹ค. 

 

๋จผ์ € ํด๋ก  ํ•  iphone setting app๊ณผ ์˜ค๋Š˜๊นŒ์ง€ ์™„์„ฑํ•œ ํด๋ก  ์•ฑ์˜ ๋ชจ์Šต์ด๋‹ค.

(์•„์ง ์ข€ ๋งŽ์ด ๋‹ค๋ฆ„)

 

 

 

 

๋จผ์ € ๋ฉ”์ธ ์Šคํ† ๋ฆฌ๋ณด๋“œ์˜ ๋ชจ์Šต์„ ๋ณด๋ฉด

main.storyboard

์ด๋ ‡๊ฒŒ ์ƒ๊ฒผ๋‹ค. ์ €๋ ‡๊ฒŒ table view๋ฅผ ํ•œ ํ™”๋ฉด ๊ฐ€๋“์ฐจ๊ฒŒ ๋จผ์ € ์ƒ์„ฑ์„ ํ•ด ๋†“๊ณ ,

๊ทธ ์•ˆ์— table view cell ์ด๋ผ๊ณ  ํ•˜๋Š” ์š”์†Œ๋ฅผ ๋„ฃ๋Š”๊ฑฐ๋‹ค.

 

์ € ์•ˆ์—์„œ ์ง์ ‘ cell๋“ค์„ ์ƒ์„ฑํ•ด์„œ ๋„ฃ์„ ์ˆ˜๋„ ์žˆ์ง€๋งŒ, 

๊ทธ๋Ÿผ ํ†ต์ผ์„ฑ๋„ ๋–จ์–ด์ง€๊ณ  ๋˜‘๊ฐ™์€ ๊ตฌ์กฐ์˜ cell๋“ค์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ๋ฏธ๋ฆฌ ํ‹€์„ ๋งŒ๋“ค์–ด ๋†“๋Š” ๊ฒƒ์ด ๋‚ซ๊ธฐ ๋•Œ๋ฌธ์—

MenuCell, ProfileCell์˜ ์ด๋ฆ„์œผ๋กœ ๋‘๊ฐ€์ง€ ๊ตฌ์กฐ์˜ cell์„ ๋งŒ๋“ ๋‹ค.

 

(์™ผ)ProfileCell, (์˜ค)MenuCell

 

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์˜ ๋ฐ˜ ํฌ๊ธฐ๋กœ ๋์„ ์ž˜๋ผ ์›์ด๋˜๊ฒŒ ๋งŒ๋“ค์—ˆ๊ณ ,

๋‚˜๋จธ์ง€ ๋ ˆ์ด๋ธ”๋“ค์€ ํฐํŠธ ํฌ๊ธฐ์™€ ์ƒ‰์„ ๋ฐ”๊ฟ” ์ฃผ๋Š” ์ž‘์—…์„ ํ–ˆ๋‹ค.

 

 

 

์ดํ›„์—๋Š” ๊ฐ๊ฐ ๋ทฐ์™€ ๋ ˆ์ด๋ธ”์— ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์–ด์„œ ๋” ๋‹ค๋“ฌ์–ด์ง„ ํด๋ก  ์•ฑ์„ ๋งŒ๋“ค๊ณ ์ž ํ•œ๋‹ค.

 

LIST

๋Œ“๊ธ€