์ด์ ์ ์ด์ด 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)
UICollectionView
๊ฐ ์๋๋๋ผ๋ ๊ณผ์ ๋ ๊ตฌํํด์ผ ํ๋, UIStackView
๋ฅผ ํ์ฉํ์ฌ ๊ณผ์ ๋ฅผ ๊ตฌ์ฑํด๋ณด๊ธฐ๋ก ํ์๋ค.
์ ๊ทธ๋ฆผ์ฒ๋ผ ์ผ๋จ์ ๋ฐ์ฏค ์ฑ๊ณตํ์๋๋ฐ, ๋์ค์ ๊ฐ Cell๋ง๋ค ์ด๋ฏธ์ง์ ๊ฐ๊ฒฉ ์ ๋ณด๋ฅผ ์ถ๊ฐํ๋ฉด ๋ ๋ฏํ๋ค!
๊ทธ๋๋ ์ฃผ๋ฌธ ๊ฐ๊ฒฉ์ ์ ์ฅํด์ ์ฌ์ฉ์์๊ฒ ์ถ๋ ฅํด์ฃผ๋ ๊ธฐ๋ฅ์ ๊ตฌํํ์๋ค!