본문 바로가기

Ios

Ios - Custom TableViewCell

1. UITableViewCell을 상속 받은 cocoa touch class 생성합니다.

이때 .xib 파일도 같이 생성합니다.(인터페이스 빌더와 같은 역할)

 

2. 인터페이스 빌더를 사용하듯이 .xib와 생성한 class 파일간에 IBOutlet, IBAction 생성하여 원하는 기능을 구현합니다.

단, 이 때 기본 UITableViewCell에 미리 정의된 프로퍼티인 textLabel, detailLabel, imageView, accessoryView와 다른 변수명을 사용하여 커스템셀 프로퍼티를 작성해주세요.

 

3. ViewDidLoad() 함수에 .xib파일의 이름으로 UINib인스턴스를 생성합니다. 생성한 UINib인스턴스와 Custom cell의 Identifier를 테이블뷰에 등록합니다.

        let nib = UINib(nibName: "CustomTableViewCell", bundle: nil)
        
        tableView.register(nib, forCellReuseIdentifier: "CustomTableViewCell")

 

4. 샐을 생성합니다. 이 때, Custom Cell의 타입으로 캐스팅을 꼭 해주어야합니다!

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell", for: indexPath) as! DemoTableViewCell
        
        cell.titleLabel.text = models[indexPath.row]
        cell.cellImageView.backgroundColor = .green
        
        return cell
    }

 

5. 완료!

'Ios' 카테고리의 다른 글

Ios - Life Cycle과 관련된 메서드들의 실행순서  (0) 2020.04.03
Ios - 간단히 알아보는 UIAlert  (0) 2020.02.11
Ios - UITableView 기본  (0) 2020.02.10
Ios - Firebase의 Authentication 사용하기  (0) 2020.01.29
Ios - CocoaPods  (0) 2020.01.23