[iOS] CollectionView Xib 파일 생성하여 Custom Cell로 구현하기

2023. 9. 5. 16:16· 🍎 Apple/iOS
목차
  1. 1. Collection view 생성
  2. 2. Custom Cell에 대한 class 생성
  3. 3. Xib 파일 생성
  4. 4. File's Owner, Custom Class 란에 방금 생성한 custom cell의 class 입력
  5. 5. 재사용 식별자 등록(여기선 "cell" 로 등록, 원하는 아무 문자로 설정해도 됨)
  6. 6. 재사용할 형태로 만들고, 추가한 요소들 IBOutlet 연결
  7. 7. collectionView를 사용하는 class 내부 didLoad 밑에 custom cell의 class 이름(여기선 CardCollectionViewCell), 재사용 식별자(여기선 cell) 넣어서 얻은 nib name 으로 collectionView.register 등록
  8. 8. xib에서 만든 custom cell을 쓸 수 있다.

1. Collection view 생성

 

2. Custom Cell에 대한 class 생성

 

3. Xib 파일 생성

 

4. File's Owner, Custom Class 란에 방금 생성한 custom cell의 class 입력

File's Owner class 설정
Custom class 설정

5. 재사용 식별자 등록(여기선 "cell" 로 등록, 원하는 아무 문자로 설정해도 됨)

 

6. 재사용할 형태로 만들고, 추가한 요소들 IBOutlet 연결

 

7. collectionView를 사용하는 class 내부 didLoad 밑에 custom cell의 class 이름(여기선 CardCollectionViewCell), 재사용 식별자(여기선 cell) 넣어서 얻은 nib name 으로 collectionView.register 등록

override func viewDidLoad() {
        super.viewDidLoad()
        
        let nibName = UINib(nibName: "CardCollectionViewCell", bundle: nil)
        self.collectionView.register(nibName, forCellWithReuseIdentifier: "cell")
 
    }

 

 

8. xib에서 만든 custom cell을 쓸 수 있다.

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
//custom cell 가져오기 
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? CardCollectionViewCell else {
            return UICollectionViewCell()
        }
 
//용도에 맞게 사용 
        if indexPath.section == 0 {
            cell.number.text = game.players[indexPath.row].name
        }
        else if let card = game.getPlayerCard(playerIndex: indexPath.row, cardIndex: indexPath.section - 1) {
            cell.fruitImage.image = UIImage(named: card.fruit.rawValue)
            cell.number.text = card.number.getNumberString() + "(\(String(indexPath.row)),\(String(indexPath.section)))"
        } else {
            cell.number.text = "(\(String(indexPath.row)),\(String(indexPath.section)))"
            cell.fruitImage.image = nil
        }
        return cell
    }

'🍎 Apple > iOS' 카테고리의 다른 글

[iOS] struct도 encoding을 해야한다.  (0) 2023.10.01
[iOS] 가로모드만 지원하도록 설정하는 방법  (0) 2023.09.25
[iOS] multiple scene 설정하기  (0) 2023.09.21
[Swift, XCTest] XCTest에서 테스트할 모듈 import 해주기  (0) 2023.08.31
[Swift, XCTest] 프로젝트에 만들었던 unit test bundle 지우기  (0) 2023.08.31
  1. 1. Collection view 생성
  2. 2. Custom Cell에 대한 class 생성
  3. 3. Xib 파일 생성
  4. 4. File's Owner, Custom Class 란에 방금 생성한 custom cell의 class 입력
  5. 5. 재사용 식별자 등록(여기선 "cell" 로 등록, 원하는 아무 문자로 설정해도 됨)
  6. 6. 재사용할 형태로 만들고, 추가한 요소들 IBOutlet 연결
  7. 7. collectionView를 사용하는 class 내부 didLoad 밑에 custom cell의 class 이름(여기선 CardCollectionViewCell), 재사용 식별자(여기선 cell) 넣어서 얻은 nib name 으로 collectionView.register 등록
  8. 8. xib에서 만든 custom cell을 쓸 수 있다.
'🍎 Apple/iOS' 카테고리의 다른 글
  • [iOS] 가로모드만 지원하도록 설정하는 방법
  • [iOS] multiple scene 설정하기
  • [Swift, XCTest] XCTest에서 테스트할 모듈 import 해주기
  • [Swift, XCTest] 프로젝트에 만들었던 unit test bundle 지우기
PushedGun
PushedGun
App Developer
내 머리에 push 중..App Developer
PushedGun
내 머리에 push 중..
PushedGun
전체
오늘
어제
  • 분류 전체보기 (27)
    • 🍎 Apple (21)
      • Swift (9)
      • iOS (12)
    • 💻 CS (2)
      • Git (2)
    • 🤿 딥다이브 (1)
    • 🤔 후기 (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • 의존성 제거
  • dequeueReusableCell
  • git
  • Xcode
  • UIKit
  • 네이버부스트캠프 멤버십
  • SWIFT
  • IOS
  • name capture
  • decode
  • swiftformat
  • designated init
  • No Swift version was specified
  • dequeueConfiguredReusableCell
  • wholeMatch
  • trailing closure
  • prefixMatch
  • Encode
  • push -f
  • convenience init

최근 댓글

최근 글

hELLO · Designed By 정상우.v4.2.0
PushedGun
[iOS] CollectionView Xib 파일 생성하여 Custom Cell로 구현하기
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.