【问题标题】:CollectionView Custom Cell Does Not Update Correctly When SearchController is Active当 SearchController 处于活动状态时,CollectionView 自定义单元格不会正确更新
【发布时间】:2020-03-11 20:19:27
【问题描述】:

我有一个带有搜索控制器和自定义单元格的集合视图。当我在搜索未激活的情况下在单元格中进行编辑时,所有内容都会毫无问题地更新。但是当搜索控制器处于活动状态时,我的编辑不会正确重新加载。这是我的一些代码。

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 也被更新为相同的项目,芦笋。

我很确定问题出在以下代码中,一旦修改完成;当搜索控制器处于活动状态时,错误的索引也会更新。知道如何更改下面的代码,以便在搜索控制器处于活动状态时更新正确的索引吗?

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()
    }

【问题讨论】:

    标签: swift iphone uitableview uicollectionview uisearchbar


    【解决方案1】:

    我通过将以下内容添加到 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)])                
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多