셀의 재사용

원동진·2024년 1월 8일

iOS-Etc

목록 보기
2/3

문제점

  • 이전에 사용하던 Cell의 UI상태가 설정되지 않았음에도 아래와 같이 C3의 t3처럼 나타남

이유 : TableView Cell의 재사용

  • 테이블뷰는 dequeueReusableCell를 통해 셀을 재사용하는데 Content를 제외하고 상태와 관련된것은 재사용함

prepareForReuse를 사용하여 Cell 초기화

  • 해당 함수를 이용하여 셀의 상태를 초기화 -> 문제점 해결
// TableView cell
override func prepareForReuse() {
        super.prepareForReuse()
        titleLabel.attributedText = NSMutableAttributedString(string: titleLabel.text!)
        dateLabel.attributedText = NSMutableAttributedString(string: dateLabel.text!)
}

-> 위코드 설명 : 이름과 날짜 label 설정을 기본으로 설정

prepareForReuse, dequeueReusableCell 뜯어보기 
profile
iOS 개발자

0개의 댓글