UICollectionView 内容无法识别(搜索时)

Posted

技术标签:

【中文标题】UICollectionView 内容无法识别(搜索时)【英文标题】:UICollectionView content not recognized (when searching) 【发布时间】:2017-03-22 08:52:59 【问题描述】:

我是 Swift 新手,我的 CollectionView 有问题。我在 CollectionView 中有 2 个自定义单元格。第一个单元格,比如cell 和第二个cellOcc,其中cell 是空表,cellOcc 是占用表。应用程序启动时它工作正常,但是当我尝试搜索它们时它变得很奇怪(内容不在它们的位置正确)。以下是我的代码,任何建议和帮助都会对我有所帮助。提前致谢。

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) 
    filteredSearchDataTable = filterArray(filteredDataTable, keywords: "tischnr", searchStr: searchText)
    tableCollectionView.reloadData()


..........

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int 
    var count:Int = 0

    if(searchBarIsActive) 
        count = filteredSearchDataTable.count
    
    else
        count = filteredDataTable.count
    

    return count

//        return max(filteredDataTable.count, 2)


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
    var cell: TableCollectionViewCell = tableCollectionView.dequeueReusableCell(withReuseIdentifier: "emptyTable", for: indexPath) as! TableCollectionViewCell
    cell.layer.borderWidth = 1
    cell.layer.cornerRadius = 5

    if (searchBarIsActive) 
        currTable = (filteredSearchDataTable[indexPath.row] as! [AnyHashable: Any])
        cell.labelTableNr.text = (String(currTable["tischnr"] as! Int))
    
    else 
        currTable = (filteredDataTable[indexPath.row] as! [AnyHashable: Any])
        cell.labelTableNr.text = (String(currTable["tischnr"] as! Int))
        currRechnr = getJSONArrayIndex(filteredDataBill, name: "tischnr", o: ((currTable["tischnr"] as! Int)))

        if self.selectedIndex.index(of: indexPath as NSIndexPath) == nil 
            //
         else 
            //
        
    

    var rechnrValue = 0

    if currRechnr >= 0 
        rechnrValue = ((filteredDataBill[currRechnr] as! [AnyHashable: Any])["rechnr"] as! Int)
        if (rechnrValue != 0) 
            let cellOcc : TableOccupiedCollectionViewCell = tableCollectionView.dequeueReusableCell(withReuseIdentifier: "occupiedTable", for: indexPath) as! TableOccupiedCollectionViewCell

            cellOcc.layer.borderWidth = 1
            cellOcc.layer.cornerRadius = 5

            cellOcc.tableNrOccLabel.text = ((filteredDataTable[indexPath.row] as! [AnyHashable: Any]) ["bezeich"] as! String)
            cellOcc.paxOccLabel.text = (String((filteredDataBill[currRechnr] as! [AnyHashable: Any]) ["belegung"] as! Int))

            cellOcc.guestNameLabel.text = ((filteredDataBill[currRechnr] as! [AnyHashable: Any]) ["bilname"] as! String)
            cellOcc.amountLabel.text = (String((filteredDataBill[currRechnr] as! [AnyHashable: Any]) ["saldo"] as! Int))

            return cellOcc
            
        else 
            return cell
        
    

    return cell

【问题讨论】:

【参考方案1】:

我解决了我的问题。在我所有的 func CollectionView 中,它总是有两种情况(searchBarActive 和正常)。我把这两种情况的逻辑都放在了一起。所以在我的最终代码下面

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
    let cell: TableCollectionViewCell = tableCollectionView.dequeueReusableCell(withReuseIdentifier: "emptyTable", for: indexPath) as! TableCollectionViewCell
    cell.layer.borderWidth = 1
    cell.layer.cornerRadius = 5

    if (searchBarIsActive) 
        currTable = (filteredSearchDataTable[indexPath.row] as! [AnyHashable: Any])
        cell.labelTableNr.text = (String(currTable["tischnr"] as! Int))

        currRechnr = getJSONArrayIndex(filteredDataBill, name: "tischnr", o: ((currTable["tischnr"] as! Int)))
        var rechnrValue = 0

        if currRechnr >= 0 
            rechnrValue = ((filteredDataBill[currRechnr] as! [AnyHashable: Any])["rechnr"] as! Int)
            if (rechnrValue != 0) 
                let cellOcc : TableOccupiedCollectionViewCell = tableCollectionView.dequeueReusableCell(withReuseIdentifier: "occupiedTable", for: indexPath) as! TableOccupiedCollectionViewCell

                cellOcc.layer.borderWidth = 1
                cellOcc.layer.cornerRadius = 5

                cellOcc.tableNrOccLabel.text = ((filteredSearchDataTable[indexPath.row] as! [AnyHashable: Any]) ["bezeich"] as! String)
                cellOcc.paxOccLabel.text = (String((filteredDataBill[currRechnr] as! [AnyHashable: Any]) ["belegung"] as! Int))

                cellOcc.guestNameLabel.text = ((filteredDataBill[currRechnr] as! [AnyHashable: Any]) ["bilname"] as! String)
                cellOcc.amountLabel.text = (String((filteredDataBill[currRechnr] as! [AnyHashable: Any]) ["saldo"] as! Int))

                return cellOcc
            
            else 
                return cell
            
        

    
    else 
        currTable = (filteredDataTable[indexPath.row] as! [AnyHashable: Any])
        cell.labelTableNr.text = (String(currTable["tischnr"] as! Int))

        currRechnr = getJSONArrayIndex(filteredDataBill, name: "tischnr", o: ((currTable["tischnr"] as! Int)))
        var rechnrValue = 0

        if currRechnr >= 0 
            rechnrValue = ((filteredDataBill[currRechnr] as! [AnyHashable: Any])["rechnr"] as! Int)
            if (rechnrValue != 0) 
                let cellOcc : TableOccupiedCollectionViewCell = tableCollectionView.dequeueReusableCell(withReuseIdentifier: "occupiedTable", for: indexPath) as! TableOccupiedCollectionViewCell

                cellOcc.layer.borderWidth = 1
                cellOcc.layer.cornerRadius = 5

                cellOcc.tableNrOccLabel.text = ((filteredDataTable[indexPath.row] as! [AnyHashable: Any]) ["bezeich"] as! String)
                cellOcc.paxOccLabel.text = (String((filteredDataBill[currRechnr] as! [AnyHashable: Any]) ["belegung"] as! Int))

                cellOcc.guestNameLabel.text = ((filteredDataBill[currRechnr] as! [AnyHashable: Any]) ["bilname"] as! String)
                cellOcc.amountLabel.text = (String((filteredDataBill[currRechnr] as! [AnyHashable: Any]) ["saldo"] as! Int))

                return cellOcc
            
            else 
                return cell
            
        

【讨论】:

以上是关于UICollectionView 内容无法识别(搜索时)的主要内容,如果未能解决你的问题,请参考以下文章

UICollectionViewCell 无法识别?

UICollectionView 无法显示所有内容

UICollectionView 是不是会滚动识别

UICollectionView:将单击手势识别器添加到补充视图

带有手势识别器的 UICollectionView

在滚动期间点击时,UICollectionView 不再识别单元格上的点击?