개발/Swift

Rx + TableView(CollectionView) 바인딩 안되는 오류 해결방법

덤벨로퍼 2023. 3. 21. 18:25

1. TableView에서 발생 (제약조건)

dataSubject.bind(to: tableView.rx.items) { tableView, row, element in
            print("Test")
            return UITableViewCell()
        }.disposed(by: disposeBag)

dataSubjecgt에 값을 아무리 집어 넣어도

위의 셀을 리턴하는 클로저가 호출이 계속안됨

tableView.register(IDIntergrationAuthCell.self, forCellReuseIdentifier: IDIntergrationAuthCell.id)
tableView.rx.setDelegate(self).disposed(by: disposeBag)

레지스터와 delegate 모두 빠지지 않은 상태 (delegate 는 안해도 됨)

dataSubject.asObservable().bind(to: authTableView.rx.items(cellIdentifier: IDIntergrationAuthCell.id,
                                         cellType: IDIntergrationAuthCell.self)) { index, item, cell in
}
dataSubject.asObservable().bind(to: authTableView.rx.items) { tableView, index, item in
            print(item)
            return UITableViewCell()
        }.disposed(by: disposeBag)

위 두가지 타입의 바인딩 모두 호출 되지않음, register 와 관련된 문제는 아닌듯 보였다.

dataSubject.bind { list in
            print("list \\(list)")
        }.disposed(by: disposeBag)

단순한 바인딩은 호출 잘됨.

subject 문제 아니고 register 문제도 아니고…

그럼? tableview 의 생성 시기가 문제인듯 했으나 binding 전에 잘 생성 되어있음

해결

Constraint 가 적절히 적용 되어있지 않았음

Constraint 때문에 해당 바인딩이 호출 안될줄은 몰랐음…

혹시나 호출이 안된다면 체크해보도록 하자

 

2. Collection view 에서 또 발생 ( 레이아웃 )

Rxdatasource + collection view 로 구현중이었다.

let data = [
            ListDialogSectionModel(title: "Section1", items: [ListDialogSectionItem(desc: "Item1")]),
            ListDialogSectionModel(title: "Section2", items: [ListDialogSectionItem(desc: "Item2")]),
            ListDialogSectionModel(title: "Section3", items: [ListDialogSectionItem(desc: "Item3")])
        ]

sectionSubject
            .bind(to: collectionView.rx.items(dataSource: datasource))
            .disposed(by: disposeBag)

self.sectionSubject.onNext(data)

위와 같이 데이터를 넣어줬으나 datasource의 configurecell이 호출 되지 않음

지난번처럼 register, constraint또한 잘 먹여놓은 상태

collectionView.snp.makeConstraints { make in
            make.top.equalTo(closebutton.snp_bottom).offset(20)
            make.leading.trailing.bottom.equalToSuperview()
        }

 

sectionSubject.bind(
            to: collectionView.rx.items(cellIdentifier: ListDialogCollectionViewCell.id,
                                        cellType: ListDialogCollectionViewCell.self)) { index, item, cell in
           print("item \\(item)")
        }.disposed(by: disposeBag)

위처럼 다르게 써봐도 역시 호출이 되지 않음...

RxCollectionViewSectionedReloadDataSource →

RxCollectionViewSectionedAnimatedDataSource

로 변경하여 적용 해도 안됨....

Cell init 함수가 구현 안되있어서 수정 해도 안됨...

 

collectionview layout을 UICollectionViewFlowLayout 으로 수정

→ !!!!!!!!!!!!!!!!! 해결 3시간 날렸네

레이아웃을 이상한걸 먹여놓고 있었다.

 

근데 이제 헤더쪽 구현하려는데 configureSupplementaryView 가 호출 안됨

이것저것 찾아보다 이것도 왠지 collectionview layout이 문제일것같아서

let layout = UICollectionViewFlowLayout()
         layout.headerReferenceSize = CGSize(width: UIScreen.main.bounds.width, height: 100)
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)

위와 같이 수정함 → 해결~

 

rx를통한 collectionview 이나 tableview의 configure cell (cell 리턴하는 클로져) 부분이 호출되지 않는다면

데이터와 바인딩이 문제일수도 있겠지만 그게 문제가 아니라면

UI를 의심해봐야한다. 레이아웃, 제약조건등의 문제가 생기면

configure cell이 호출되지 않는다.