Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios swift-如何筛选我的自定义类?_Ios_Swift_Uicollectionview_Uisearchbar - Fatal编程技术网

Ios swift-如何筛选我的自定义类?

Ios swift-如何筛选我的自定义类?,ios,swift,uicollectionview,uisearchbar,Ios,Swift,Uicollectionview,Uisearchbar,我用字符串、url和int值生成类,并生成通道数组 但我无法对我的通道阵列进行良好的过滤 这是我的班级: 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和int值生成类,并生成通道数组 但我无法对我的通道阵列进行良好的过滤

这是我的班级:

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({$0.name.lowercased().prefix(searchText.count) == searchText.lowercased()})

            collectionView.reloadData()
        }
    }

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

更新
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
  }

此行(
如果let channel=channels[indexPath.item]作为通道?{
)导致您总是从包含所有元素的非筛选数组中获取
频道
对象(即使在搜索过程中也是如此)的问题。因此,您应该如上所述从相应的
数组
中获取
频道
对象,以正确显示
结果

谢谢,但我的搜索功能仍然不可用工作。验证您的
filteredData
列表是否具有正确的结果。