idghst.dev
article thumbnail
Published 2023. 2. 6. 09:00
[ iOS ] CollectionView - 1 iOS
728x90
반응형

ViewController 연결

…ViewController.swift 생성 후
스토리보드의 ViewController에서
Custom Class 의 Class 와
Identity의 Storyboard ID 를 수정한다

 

최초 로딩 관련 함수

viewWillAppear → viewDidLoad → viewDidAppear

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    // print("viewWillAppear")
}

override func viewDidLoad() {
    super.viewDidLoad()
    // print("viewDidLoad")
    reload()

    // Do any additional setup after loading the view.
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    // print("viewDidAppear")
}

 

CollectionView 셀 데이터

extension [앱id]ViewController: UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return stockList.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "[앱id]CollectionViewCell", for: indexPath)
        as? [앱id]CollectionViewCell
        else {  return UICollectionViewCell() }

        let [앱id] = [앱id]List[indexPath.item]
        cell.configure(stock)

        return cell
    }
}

 

CollectionView 셀 배치

extension [앱id]ViewController: UICollectionViewDelegateFlowLayout{
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: collectionView.bounds.width, height: 80)
    }
}

 

CollectionViewCell 자동 호출 함수

override func awakeFromNib() {
    super.awakeFromNib()
}

 

CollectionViewCell 데이터 설정 함수

func configure(_ chat: Chat) {
    thumbnailImageView.image = UIImage(named: chat.name)
    nameLabel.text = chat.name
    chatLabel.text = chat.chat
    dateLabel.text = formattedDateString(dateString: chat.date)
}

 

그리드 분할 관련 초기화

  • 설정이 되어 있지 않으면 분할되지 않을 수 있음
if let flowlayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
    flowlayout.estimatedItemSize = .zero
}

 

CollectionView 그리드 크기 설정

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: collectionView.bounds.width, height: 100)
}

 

그리드 아이템 클릭 관련 함수

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let framework = list[indexPath.item]
    print(">>> selected: \(framework.name)")
}
728x90
반응형

'iOS' 카테고리의 다른 글

[ iOS ] Modal  (0) 2023.02.24
[ iOS ] CollectionView - 3  (0) 2023.02.22
[ iOS ] PagingView  (0) 2023.02.20
[ iOS ] CollectionView - 2  (0) 2023.02.17
[ iOS ] Assistant 모드  (0) 2023.02.13
profile

idghst.dev

@idghst.dev

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!