본문 바로가기

iOS

[Swift] dequeueReusableCell

반응형

테이블 뷰에서 셀을 추가할때 사용하는 함수.

다만 메모리 절약을 위해 스크롤 등으로 화면이 옮겨질때, 이전에 사용되었던 셀이 재사용된다.

 

그런대 여기서 문제 하나가 재사용되는 셀을 제대로 초기화를 해주지 않는다면 화면이 옮겨지고 난 이후에

버튼등이 제대로 표현되지 않는다.

 

따라서 셀 내부에 버튼 등 이벤트들은 따로 데이터를 저장해 줄 필요가 있고, 그 데이터를 활용하여

항상 초기화를 해주어야한다.

 

//Table View
guard let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCellCharacteristic", for: indexPath)
                    as? CharacteristicTableViewCell else { return UITableViewCell() }

let currentNotifyFlag = tvData[indexPath.section].characteristicNotifyFlag[indexPath.row - 1]

if currentNotifyFlag {
    cell.btnNotice.isSelected = true
}

cell.configureUI(properties: properties)
            
return cell


//Table View Cell
override func prepareForReuse() {
    super.prepareForReuse()

    print("_hyeon prepareForReuse: ", ServiceViewControl().tagSectionRowTable)
    btnNotice.isSelected = false
}

'iOS' 카테고리의 다른 글

[Swift] Firebase data read  (0) 2022.02.16
개발자 등록.  (0) 2022.02.15
[Swift] 함수(Function)?  (0) 2022.01.27
[Swift] contentView  (0) 2022.01.24
[Swift] isUserInteractionEnabled  (0) 2022.01.24