Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.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 tableview不能使用搜索栏_Ios_Swift_Uitableview - Fatal编程技术网

Ios 带节的swift tableview不能使用搜索栏

Ios 带节的swift tableview不能使用搜索栏,ios,swift,uitableview,Ios,Swift,Uitableview,我已经用searchbar创建了一个tableview。我的数据集如下所示: var data : [[ContactObject]] = [] 一切都很好,但如果我试图搜索它,它不会真正起作用。 以下是我的搜索方法: func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { filteredGroups = self.data[1].filter({(bo: ContactO

我已经用searchbar创建了一个tableview。我的数据集如下所示:

    var data : [[ContactObject]] = []
一切都很好,但如果我试图搜索它,它不会真正起作用。 以下是我的搜索方法:

  func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    filteredGroups = self.data[1].filter({(bo:  ContactObject ) -> Bool in
               return bo.name!.lowercased().contains(searchText.lowercased())
            })
    filteredUsers = self.data[2].filter({(bo:  ContactObject ) -> Bool in
        return bo.name!.lowercased().contains(searchText.lowercased())
     })
    self.filteredData.append(self.myStories)
    self.filteredData.append(self.filteredGroups  )
    self.filteredData.append(self.filteredUsers)
     collectionView.reloadData()
 }
我总是添加self.myStories,因为它在我的tableview中是静态内容。为了显示合适的数据,我已将单元格扩展为项目,如下所示:

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if !isFiltering(){
    if data[indexPath.section][indexPath.row].name == "Freunde" || data[indexPath.section][indexPath.row].name == "Interessen (öffentlich)"{
       var cell1 = tableView.dequeueReusableCell(withIdentifier: "selectStory", for: indexPath) as! StorySelectionTableViewCell

        cell1.label.text = data[indexPath.section][indexPath.row].name
        cell1.select.setOn(false, animated: true)
        cell1.select.tag = indexPath.row
        cell1.select.addTarget(self, action: #selector(handleSwitch), for: .valueChanged)
        return cell1
    }

        var cell = tableView.dequeueReusableCell(withIdentifier: "contactCell", for: indexPath) as! ContactsTableViewCell
        cell.select.tag = indexPath.row
        cell.thumb.layer.masksToBounds = false
        cell.thumb.layer.cornerRadius = cell.thumb.frame.height/2
        cell.thumb.clipsToBounds = true
        cell.name.text =  data[indexPath.section][indexPath.row].name
        cell.select.addTarget(self, action: #selector(handleButtonPress), for: .touchDown)
        if data[indexPath.section][indexPath.row].imageUrl != "" && data[indexPath.section][indexPath.row].imageUrl != nil{

            let url = URL(string:  data[indexPath.section][indexPath.row].imageUrl!)
            cell.thumb.kf.setImage(with: url)

                }
        return cell

    }else{



        if filteredData[indexPath.section][indexPath.row].name == "Freunde" || filteredData[indexPath.section][indexPath.row].name == "Interessen (öffentlich)"{
                var cell1 = tableView.dequeueReusableCell(withIdentifier: "selectStory", for: indexPath) as! StorySelectionTableViewCell

                 cell1.label.text = filteredData[indexPath.section][indexPath.row].name
                 cell1.select.setOn(false, animated: true)
                 cell1.select.tag = indexPath.row
                 cell1.select.addTarget(self, action: #selector(handleSwitch), for: .valueChanged)
                 return cell1
             }

                 var cell = tableView.dequeueReusableCell(withIdentifier: "contactCell", for: indexPath) as! ContactsTableViewCell
                 cell.select.tag = indexPath.row
                 cell.thumb.layer.masksToBounds = false
                 cell.thumb.layer.cornerRadius = cell.thumb.frame.height/2
                 cell.thumb.clipsToBounds = true
                 cell.name.text =  filteredData[indexPath.section][indexPath.row].name
                 cell.select.addTarget(self, action: #selector(handleButtonPress), for: .touchDown)
                 if data[indexPath.section][indexPath.row].imageUrl != "" && data[indexPath.section][indexPath.row].imageUrl != nil{

                     let url = URL(string:  filteredData[indexPath.section][indexPath.row].imageUrl!)
                     cell.thumb.kf.setImage(with: url)

                         }
                 return cell

    }


}
我的失业人数是这样的:

   func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if isFiltering(){
        return filteredData[section].count
    }
return data[section].count
}

结果是,无论我输入哪个单词,我的第三部分(self.filteredUsers)总是空的,self.filteredGroups总是完整的。

这只是一个猜测,但我认为你不应该在这里附加到
filteredData

self.filteredData.append(self.myStories)
self.filteredData.append(self.filteredGroups  )
self.filteredData.append(self.filteredUsers)
这将使
filteredData
越来越长,而
cellForRowAt
仅使用前三项。您应该用三个子阵列替换整个阵列:

filteredData = [myStories, filteredGroups, filteredUsers]

我注意到的另一件事是,在上面三行之后重新加载集合视图,但搜索栏似乎安装在表视图上。也许您应该重新加载表视图,否则这只是一个打字错误。

非常感谢。你能给我解释一下原因吗difference@Silas您最初的方法是“将这三个数组添加到嵌套数组”,而我将其更改为“仅将嵌套数组的内容替换为三个数组”。使用您的方法,
filteredata
会随着您不断添加内容而任意增长。