TableViewCell ๋ฐ์ดํฐ ์ค์
Cell์ ๋ฐ์ดํฐ๋ฅผ ๋ฃ์ด์ค์ผ ํ๋ ๊ฒฝ์ฐ๊ฐ ์๋ค. ๊ฐ๋จํ๊ฒ ์๊ฐ์ ํด๋ณด๋ฉด CellForRowAt ๋ฉ์๋ ๋จ๊ณ์์ Cell์ ํ๋กํผํฐ์ ์ ๊ทผํ์ฌ ๋ฐ์ดํฐ๋ฅผ ํ๋์ฉ ๋ฃ์ด์ค ์ ์์ ๊ฒ์ด๋ค. ๊ทธ๋ฐ๋ฐ ๋ฃ์ด์ค์ผ ํ๋ ๋ฐ์ดํฐ๊ฐ ์ฌ๋ฌ ๊ฐ์ธ ๊ฒฝ์ฐ ํ๋์ฉ ๋ค ๋ฃ์ด์ฃผ๊ธฐ๊ฐ ์ ๋ง ๋ฒ๊ฑฐ๋กญ๊ณ ๋นํจ์จ์ ์ธ ์์ ์ด ๋ ๊ฒ์ด๋ค.
์ด ์์ ์ Cell์๊ฒ ๋์ ๋ถ์ฌํด์ ๋ฐ์ดํฐ๋ง ํต์งธ๋ก ๋๊ฒจ์ฃผ๋ฉด Cell์ด ์์์ UI ์์์ ๋ฐ์ดํฐ๋ฅผ ๋ฃ์ด์ฃผ๋๋ก ํ๋ฉด ๊น๋ํด์ง ๊ฒ์ด๋ค. ๋ฐ์ดํฐ ์ญ์ ์ธ์๋ก ํ๋์ฉ ๋๊ฒจ์ค ์ ์๊ฒ ์ง๋ง ๊ทธ ์๊ฐ ๋ง๋ค๋ฉด ํ๋ค ๊ฒ์ด๋ค. ๊ทธ๋์ ๊ตฌ์กฐ์ฒด ํํ๋ก ์ฌ๋ฌ ํ์ ์ ๋ฐ์ดํฐ๋ฅผ ํ ๋ฌถ์์ผ๋ก ์๊ฐํด์ ๋ฐ์ดํฐ๋ฅผ ๋๊ฒจ์ฃผ๋ฉด ํธํ ๊ฒ์ด๋ค.
// Model Struct
struct Movie {
var title: String
var releaseDate: String
var description: String
var runtime: Int
var rate: Double
}
extension Movie {
static let sampleData: [Movie] = [
Movie(title: "์์ด", releaseDate: "22.22.22", description: "์ค๊ฑฐ๋ฆฌ", runtime: 150, rate: 8.8),
Movie(title: "๊ดด๋ฌผ", releaseDate: "22.22.22", description: "์ค๊ฑฐ๋ฆฌ", runtime: 150, rate: 8.8),
Movie(title: "๊ด์", releaseDate: "22.22.22", description: "์ค๊ฑฐ๋ฆฌ", runtime: 150, rate: 8.8),
Movie(title: "ํ๊ฑด", releaseDate: "22.22.22", description: "์ค๊ฑฐ๋ฆฌ", runtime: 150, rate: 8.8),
Movie(title: "๋ง๋
", releaseDate: "22.22.22", description: "์ค๊ฑฐ๋ฆฌ", runtime: 150, rate: 8.8),
Movie(title: "๊ธฐ์์ถฉ", releaseDate: "22.22.22", description: "์ค๊ฑฐ๋ฆฌ", runtime: 150, rate: 8.8),
Movie(title: "๊ดํด", releaseDate: "22.22.22", description: "์ค๊ฑฐ๋ฆฌ", runtime: 150, rate: 8.8),
Movie(title: "๋ช
๋", releaseDate: "22.22.22", description: "์ค๊ฑฐ๋ฆฌ", runtime: 150, rate: 8.8),
Movie(title: "์๋ํ ์ผ๋งจ", releaseDate: "22.22.22", description: "์ค๊ฑฐ๋ฆฌ", runtime: 150, rate: 8.8)
]
}
// Cell
import UIKit
final class MovieCell: UITableViewCell {
@IBOutlet weak var posterImageView: UIImageView!
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var releaseLabel: UILabel!
@IBOutlet weak var descriptionLabel: UILabel!
func configureCell(data: Movie) {
self.titleLabel.text = data.title
self.releaseLabel.text = data.releaseDate
self.descriptionLabel.text = data.description
}
}
// Cell For Row At
private var movieList: [Movie] = Movie.sampleData
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MovieCell") as! MovieCell
cell.configureCell(data: movieList[indexPath.row])
return cell
}
CollectionView
ํ ์ด๋ธ ๋ทฐ๋ฅผ ๊ตฌ์ฑํ๋ ๊ฒ๊ณผ ๋๋ฌด ๋์ผํ๋ค. ์ฐจ์ด๋ ํ ์ด๋ธ ๋ทฐ์ ๋ฌ๋ฆฌ ์ฌ๋ฌ ๊ฐ์ ์ด์ ์์ดํ ์ ์ง์ด ๋ฃ์ ์ ์๊ธฐ ๋๋ฌธ์ ํ๋์ ์ (์์ดํ )์ ๋๋น์ ๋์ด, ์ฆ ์์ดํ ์ ์ฌ์ด์ฆ๋ฅผ ๊ฒฐ์ ํด์ฃผ๋ ์์ ์ด ํ์ํ๋ค.
- ์ํ ์คํฌ๋กค์ ์์ดํ ์ ์์ง(์ธ๋ก) ๋ฐฉํฅ์ผ๋ก ๋จผ์ ์ ์ ์ฑ์ด๋ค.
- ์์ง ์คํฌ๋กค์ ์์ดํ ์ ์ํ(๊ฐ๋ก) ๋ฐฉํฅ์ผ๋ก ๋จผ์ ์ ์ ์ฑ์ด๋ค.
let layout = UICollectionViewFlowLayout()
let spacing: CGFloat = 8
let width = UIScreen.main.bounds.width - (spacing * 4)
layout.itemSize = CGSize(width: width / 3, height: 150)
layout.scrollDirection = .vertical
layout.sectionInset = UIEdgeInsets(top: spacing, left: spacing, bottom: spacing, right: spacing)
layout.minimumLineSpacing = spacing
layout.minimumInteritemSpacing = spacing
collectionView.collectionViewLayout = layout
๋ผ์ด๋ธ๋ฌ๋ฆฌ ์๊ฐ
- Kingfisher (์ด๋ฏธ์ง, ์ด๋ฏธ์ง ์บ์ฑ ์ฒ๋ฆฌ)
- ToastSwift (ํ ์คํธ ๋ฉ์์ง, UI ๋ผ์ด๋ธ๋ฌ๋ฆฌ)
'๐ฑ SeSAC' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[SeSAC] 220722 TIL (0) | 2022.07.26 |
---|---|
[SeSAC] 220721 TIL (0) | 2022.07.21 |
[SeSAC] 220719 TIL (0) | 2022.07.21 |
[SeSAC] 220718 TIL (0) | 2022.07.21 |
[SeSAC] 220715 TIL (0) | 2022.07.21 |