[๐Ÿ“ iOS TIL] #18 ... 1st Team Project - Coffee Ordering App #2

TaeUkยท2024๋…„ 4์›” 2์ผ
0

๐ŸŽ iOS TIL

๋ชฉ๋ก ๋ณด๊ธฐ
17/18
post-thumbnail

์˜ค๋Š˜ ์‹œ๋„ํ•ด ๋ณธ ๊ฒƒ๋“ค!

์–ด์ œ์— ์ด์–ด UICollectionView๋ฅผ ํ™œ์šฉํ•˜๋Š” ํ™”๋ฉด ๊ตฌ์„ฑ์„ ๊ณ„์† ์‹œ๋„ํ•ด๋ณด์•˜๋‹ค.

CustomView๋ฅผ ํ™œ์šฉํ•˜์—ฌ ํ•˜๋‚˜์˜ ๋””์ž์ธ๋œ Cell๋ฅผ ๋งŒ๋“ค์–ด ์žฌ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด, ์–ด์ œ์™€ ์œ ์‚ฌํ•˜๊ฒŒ ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜์˜€๋‹ค.

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var CoffeeListSelector: UISegmentedControl!
    @IBOutlet weak var CoffeListCollection: UICollectionView!
    
    let coffeeDataSet = [
        ["name": "์•„๋ฉ”๋ฆฌ์นด๋…ธ", "image": "coffee1.jpg", "price": "2500"],
        ["name": "์นดํŽ˜๋ผ๋–ผ", "image": "coffee2.jpg", "price": "4000"],
        ["name": "์—์Šคํ”„๋ ˆ์†Œ", "image": "coffee3.jpg", "price": "2000"]
    ]

    let teaDataSet = [
        ["name": "์ผ€๋ชจ๋งˆ์ผํ‹ฐ", "image": "tea1.jpg", "price": "4000"],
        ["name": "์•„์ด์Šคํ‹ฐ", "image": "tea2.jpg", "price": "3500"],
        ["name": "๋ ˆ๋ชฌํ‹ฐ", "image": "tea3.jpg", "price": "4000"],
        ["name": "๊ทธ๋ฆฐํ‹ฐ", "image": "tea4.jpg", "price": "3500"]
    ]

    let cakeDataSet = [
        ["name": "๋ธ”๋ฃจ๋ฒ ๋ฆฌ์น˜์ฆˆ์ผ€์ต", "image": "cake1.jpg", "price": "6000"],
        ["name": "๋ฐ”์Šคํฌ ์น˜์ฆˆ์ผ€์ต", "image": "cake2.jpg", "price": "5500"],
        ["name": "๋‹น๊ทผ ์ผ€์ต", "image": "cake3.jpg", "price": "6500"]
    ]

    var currentDataSet: [[String : String]] = []
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        CoffeeListSelector.addTarget(self, action: #selector(segmentedControlValueChanged(_:)), for: .valueChanged)
        
        CoffeListCollection.isHidden = false
        
        currentDataSet = coffeeDataSet
    }
    
    
    @objc func segmentedControlValueChanged(_ sender: UISegmentedControl) {
        switch sender.selectedSegmentIndex {
        case 0:
            currentDataSet = coffeeDataSet
        case 1:
            currentDataSet = teaDataSet
        case 2:
            currentDataSet = cakeDataSet
        default:
            break
        }
        CoffeListCollection.reloadData()
    }
}



extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return currentDataSet.count
    }
    
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CoffeeListCollectionCell
        
        let item = currentDataSet[indexPath.item]
        
        if let imageName = item["image"], let image = UIImage(named: imageName) {
            cell.coffeeImage.image = image
        }
        
        if let name = item["name"] {
            cell.coffeeLabel.text = name
        }
        
        return cell
    }

}

// Custom Cell
import UIKit

class CoffeeListCollectionCell: UICollectionViewCell {
    @IBOutlet weak var coffeeImage: UIImageView!
    @IBOutlet weak var coffeeLabel: UILabel!
}

์œ„ ์ฝ”๋“œ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ StoryBoard์˜ UI์š”์†Œ์™€ ViewController์™€์˜ ์—ฐ๊ฒฐ์„ ๋ฌธ์ œ์—†์ด ๊ตฌ์„ฑํ•˜์˜€๋‹ค!

ํ•˜์ง€๋งŒ... ์™œ์ธ์ง€ UICollectionView๋Š” ๋™์ž‘์„ ํ•˜์˜€์ง€๋งŒ, Cell์€ ์ถœ๋ ฅ๋˜์ง€ ์•Š์•˜๋‹ค...

์œ„ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ ๊ฐ๊ฐ์˜ ์š”์†Œ๋งˆ๋‹ค BackGroundColor๋ฅผ ์ ์šฉํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ด๋ณด๋‹ˆ, Cell๋งŒ ์ถœ๋ ฅ๋˜์ง€ ์•Š๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค... ๐Ÿฅฒ

๊ทธ๋ž˜์„œ ์ผ๋‹จ์€ UICollectionView๋ฅผ ํ™œ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์ž ์‹œ ํฌ๊ธฐํ•˜๊ณ  ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ์ฐพ์•„๋ณด๊ธฐ๋กœ ํ•˜์˜€๋‹ค.

์ฐธ๊ณ  ์ž๋ฃŒ : [iOS - swift] 13. ์ปฌ๋ ‰์…˜ ๋ทฐ(Collection View)


UIStackView๋กœ ๋Œ€์ฒดํ•ด๋ณด๊ธฐ!

UICollectionView๊ฐ€ ์•ˆ๋˜๋”๋ผ๋„ ๊ณผ์ œ๋Š” ๊ตฌํ˜„ํ•ด์•ผ ํ•˜๋‹ˆ, UIStackView๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๊ณผ์ œ๋ฅผ ๊ตฌ์„ฑํ•ด๋ณด๊ธฐ๋กœ ํ•˜์˜€๋‹ค.

์œ„ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ ์ผ๋‹จ์€ ๋ฐ˜์ฏค ์„ฑ๊ณตํ•˜์˜€๋Š”๋ฐ, ๋‚˜์ค‘์— ๊ฐ Cell๋งˆ๋‹ค ์ด๋ฏธ์ง€์™€ ๊ฐ€๊ฒฉ ์ •๋ณด๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ๋  ๋“ฏํ•˜๋‹ค!

๊ทธ๋ž˜๋„ ์ฃผ๋ฌธ ๊ฐ€๊ฒฉ์„ ์ €์žฅํ•ด์„œ ์‚ฌ์šฉ์ž์—๊ฒŒ ์ถœ๋ ฅํ•ด์ฃผ๋Š” ๊ธฐ๋Šฅ์€ ๊ตฌํ˜„ํ•˜์˜€๋‹ค!

0๊ฐœ์˜ ๋Œ“๊ธ€