Conversation
longlivedrgn
left a comment
There was a problem hiding this comment.
주현 고생했어!!~! 코멘트 같이 고민해보면 좋을 거 몇개만 달았어!💪
|
|
||
| extension SelectCardView: UICollectionViewDataSource { | ||
|
|
||
| func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { | ||
| return 10 | ||
| } | ||
|
|
||
| func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { | ||
| guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CardImageCollectionViewCell.className, for: indexPath) as? CardImageCollectionViewCell else { return UICollectionViewCell() } | ||
|
|
||
| if indexPath.item == 0 { | ||
| cell.isSelected = true | ||
| collectionView.selectItem(at: indexPath, animated: false, scrollPosition: .init()) | ||
| } | ||
| // cell.setData(with: "") | ||
| return cell | ||
| } | ||
|
|
||
| } |
There was a problem hiding this comment.
주현 요거 아래와 같은 오류가 있는거 같아!
뭔가 다시 dequeue되면서 index가 0인걸로 select가 되는 거 같더라구!
사실 지금은 서버 통해서 image 받는 받는 게 따로 없어서 그런 거 같긴한데, rxswift를 활용하니까 cell deque하는 코드는 아래와 같이 바꿔도 괜찮을 거 같아!
Observable
.bind(to: collectionView.rx
.items(cellIdentifier: "colorCell", cellType: CollectionViewCell.self)) { index, color, cell in
cell.label.text = "Good"
}
.disposed(by: bag)또, 맨 위의 문제랑 연관된 거일 수도 있는데, 특정 cell이 select되었을 때는 특정 cell 안에 있는 isSelected 구문을 직접 설정하는 것보다 delegate 메소드로 select된 index의 cell의 색깔을 직접 바꿔주는 것도 좋을 거 같오!
collectionView.rx.itemSelected
.subscribe(onNext: { index in
print("\(index.section) \(index.row)")
})
.disposed(by: bag)There was a problem hiding this comment.
덕분에 셀 재사용 dequeue되면서 저 함수가 계속 호출되는 오류 확인했다!! 고마워 🙏
cell.isSelected = true 이 코드를 썼던 이유는 초기 설정 값을 주기 위함이었어! 처음 선물하기 상세로 들어갔을 때 카드 이미지의 0번 인덱스가 설정되어있어야 하더라구
그래서 수정하면서 두 가지 방법을 생각했어!
- indexPathsForSelectedItems를 사용해서 선택된 item을 배열로 받고 isEmpty일 경우 0번 인덱스를 선택해주기
- willDisplayCell을 사용하고 take(1)으로 0번 cell이 생성될 때 딱 한 번만 선택해주기
이 중에서 1번 indexPathsForSelectedItems을 사용하다 보니 cell을 가져오기가 애매하더라고! 그래서 willDisplayCell을 사용해서
self.selectCardView.cardImageCollectionView.rx.willDisplayCell
.take(1)
.bind(with: self) { owner, item in
item.cell.isSelected = true
owner.selectCardView.cardImageCollectionView.selectItem(at: item.at, animated: false, scrollPosition: .left)
owner.viewModel.input.selectedImageIndex.accept(item.at.item)
}
.disposed(by: self.disposeBag)이렇게 처리해줬어! item을 프린트 해보니까 cell(item.cell)이랑 indexPath값(item.at)을 가져올 수 있었어 👍
작업한 내용
스크린샷
관련 이슈