swift - 如何过滤我的自定义 class?

swift - how to filtering my custom class?

我用字符串、url 和 int 值制作 class 并制作通道数组 但是我无法对我的通道数组进行良好的过滤

这是我的class:

class Channel {
    var name: String
    var url: URL
    var icon: URL?
    var priority: Int

    init(name:String, url:URL, icon:URL?, priority:Int) {
        self.name = name
        self.url = url
        self.icon = icon
        self.priority = priority
    }
}

我正在尝试这个功能,但它不起作用!

public var channels: [Channel] = []
    var filteredData = [Channel]()
    var searching = false
...

 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if searching {
            return filteredData.count
        }
        return channels.count

    }

   func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if let channel = channels[indexPath.item] as Channel? {
            if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? CollectionViewCell {

                if searching {
                    cell.name.text = filteredData[indexPath.row].name
                    print("fil \(filteredData[indexPath.row].name)")
                } else {
                    cell.name.text = channel.name
                    print("not \(channel.name)")

                }


                if let iconUrl = channel.icon {
                    cell.icon.cacheSetImageFromURL(iconUrl)
                } else {
                    cell.icon.image = #imageLiteral(resourceName: "tv1600")
                }
                return cell
            }
        }

        return UICollectionViewCell()

    }

...

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

        if searchBar.text == nil || searchBar.text == "" {
            searching = false
            collectionView.reloadData()
        }else{
            print("searching")
            searching = true

            filteredData = channels.filter({[=13=].name.lowercased().prefix(searchText.count) == searchText.lowercased()})

            collectionView.reloadData()
        }
    }

我的预期结果显示过滤后的数组以及我的 CollctionView 中的现有内容。

更新 cellForItemAt 如下,

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell

        let channel = self.searching ? filteredData[indexPath.row] : channels[indexPath.item]
        cell.name.text = channel.name
        print("fil \(channel.name)")

        if let iconUrl = channel.icon {
            cell.icon.cacheSetImageFromURL(iconUrl)
        } else {
            cell.icon.image = #imageLiteral(resourceName: "tv1600")
        }
        return cell
  }

这一行 (if let channel = channels[indexPath.item] as Channel? {) 导致的问题是,即使在搜索过程中,您总是从包含所有元素的未过滤数组中获取 channel 对象。所以你应该从上面相应的 array 中取出 channel 对象来正确显示 results.