searchcontroller와 사용자 지정 셀이있는 collectionview가 있습니다. 검색이 활성화되지 않은 상태에서 셀을 편집하면 모든 것이 문제없이 업데이트됩니다. 그러나 searchcontroller가 활성화되면 편집 내용이 올바르게 다시로드되지 않습니다. 다음은 내 코드 중 일부입니다.
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//SEARCH
if searchController.isActive{
return searchResults.count
}else{
return products.count
}
//SEARCH
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Identifiers.ProductCell, for: indexPath) as? ProductCell {
//SEARCH
// Determine if we get the products from search result or the original array
let searchItem = (searchController.isActive) ? searchResults[indexPath.item] : products[indexPath.item]
cell.product = searchItem
cell.delegate = self
return cell
}
return UICollectionViewCell()
}
컬렉션의 셀에 정보를 채우기위한 리스너가 있으며 여기에 리스너에 대한 코드가 있습니다.
func setProductsListener(){
listener = ProductsService.getProducts { [weak self] change, product in
guard let self = self else { return }
switch change.type {
case .added:
self.onDocumentAdded(change: change, product: product)
case .modified:
self.onDocumentModified(change: change, product: product)
case .removed:
self.onDocumentRemoved(change: change)
@unknown default: break
}
}
}
내 변경 사항이 collectionView에 다시로드되는 곳은 다음과 같습니다.
func onDocumentAdded(change: ProductChange, product: Product){
let newIndex = Int(change.newIndex)
products.insert(product, at: newIndex)
collectionView.insertItems(at: [IndexPath(item: newIndex, section: 0)])
// calculate and set title for cart subtotal
cartBtn.setTitle(subtotal.penniesToFormattedCurrency(), for: .normal)
cartBtn.sizeToFit()
}
func onDocumentModified(change: ProductChange, product: Product) {
if change.newIndex == change.oldIndex {
// Item changed, but remained in the same position
let index = Int(change.newIndex)
products[index] = product
(collectionView.cellForItem(at: IndexPath(item: index, section: 0)) as? ProductCell)?.product = product
// calculate and set title for cart subtotal
cartBtn.setTitle(subtotal.penniesToFormattedCurrency(), for: .normal)
cartBtn.sizeToFit()
} else {
// Item changed and changed position
let oldIndex = Int(change.oldIndex)
let newIndex = Int(change.newIndex)
products.remove(at: oldIndex)
products.insert(product, at: newIndex)
collectionView.moveItem(at: IndexPath(item: oldIndex, section: 0), to: IndexPath(item: newIndex, section: 0))
// calculate and set title for cart subtotal
cartBtn.setTitle(subtotal.penniesToFormattedCurrency(), for: .normal)
cartBtn.sizeToFit()
}
}
func onDocumentRemoved(change: ProductChange){
let oldIndex = Int(change.oldIndex)
products.remove(at: Int(oldIndex))
collectionView.deleteItems(at: [IndexPath(item: oldIndex, section: 0)])
// calculate and set title for cart subtotal
cartBtn.setTitle(subtotal.penniesToFormattedCurrency(), for: .normal)
cartBtn.sizeToFit()
}
검색 컨트롤러가 활성화되지 않았을 때의 UI는 다음과 같습니다.
검색을 시작하고 "As .."라는 단어를 입력하면 (Asparagus 검색 시도) 다음 결과가 나타납니다.
지금까지는 문제가 없습니다. 그러나 검색 컨트롤러가 활성화 된 상태에서 수량을 변경하기 위해 "플러스"또는 "마이너스"를 클릭하자마자; 잘못된 색인도 업데이트됩니다. 아래 스크린 샷을보십시오. Asparagus에 대해 인덱스 0 만 업데이트되는 것은 아닙니다. 인덱스 2도 동일한 항목 인 Asparagus로 업데이트됩니다.
수정이 완료되면 다음 코드에 문제가 있다고 확신합니다. 검색 컨트롤러가 활성화되면 잘못된 인덱스도 업데이트됩니다. 검색 컨트롤러가 활성화 될 때 올바른 인덱스가 업데이트되도록 아래 코드를 어떻게 변경할 수 있는지 아십니까?
func onDocumentModified(change: ProductChange, product: Product) {
if change.newIndex == change.oldIndex {
// Item changed, but remained in the same position
let index = Int(change.newIndex)
products[index] = product
(collectionView.cellForItem(at: IndexPath(item: index, section: 0)) as? ProductCell)?.product = product
// calculate and set title for cart subtotal
cartBtn.setTitle(subtotal.penniesToFormattedCurrency(), for: .normal)
cartBtn.sizeToFit()
}
다음을 추가하여 수정했습니다 func onDocumentModified
.
if searchController.isActive {
for item in searchResults {
if item.id == product.id {
let searchedItemIndex = Int((searchResults.firstIndex(of: item))!)
(collectionView.cellForItem(at: IndexPath(item: searchedItemIndex, section: 0)) as? ProductCell)?.product = product
}
}
} else {
collectionView.reloadItems(at: [IndexPath(item: index, section: 0)])
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다