Swift & iOS

[Swift& iOS] View reload 관련 에러

jkkooooooo 2022. 2. 8. 23:02
반응형

RxSwift로 CollectionView를 reloadData() 할 때 아래와 같은 에러가 발생하였습니다. 

 

Maybe delegate was already set in xib or storyboard and now it's being overwritten in code.

 

에러 내용은 간단합니다. 이미 delegate나 설정 되어있다는것입니다.

 

 

저 같은 경우 이미 Observable bind를 통해 CollectionView를 생성하였고 

func fetchData() {
    output.showGearDetail
            .do{ self.gearDetail = $0 }
            .map{ $0.images }
            .do{
                self.pageControl.numberOfPages = $0.count
                self.pageControl.reloadInputViews()
            }
            .bind(to:gearDetailCollectionView.rx.items(cellIdentifier: GearDetailImageCollectionViewCell.identifier, cellType: GearDetailImageCollectionViewCell.self)) { (row, element, cell) in
                cell.onData.onNext(element)
            }.disposed(by: disposeBag)
}

다른 뷰컨트롤러에서 NotificationCenter로 fetchData()를 호출 하니 에러가 발생한것이였습니다.

 

해결방법도 간단합니다!! bind를 하기전에 생성할 CollectionView에 datasource와 delegate를 nil로 초기화 시켜주면 됩니다.

gearDetailCollectionView.dataSource = nil
gearDetailCollectionView.delegate = nil

 

nil로 초기화 시킨 후 다시 bind를 해주면 됩니다.

반응형