๐ŸŒฑ SeSAC

[SeSAC] 220720 TIL

taeeekki 2022. 7. 21. 23:09

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

 


 

๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์†Œ๊ฐœ

  1. Kingfisher (์ด๋ฏธ์ง€, ์ด๋ฏธ์ง€ ์บ์‹ฑ ์ฒ˜๋ฆฌ)
  2. ToastSwift (ํ† ์ŠคํŠธ ๋ฉ”์‹œ์ง€, UI ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ)