带有部分的快速tableview不能使用搜索栏

时间:2019-10-29 16:42:01

标签: ios swift uitableview

我用搜索栏创建了一个表格视图。我的数据集如下所示:

    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,因为它的静态内容在我的表视图中。为了显示合适的数据,我将项目的单元格扩展如下:

 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

    }


}

和我的numberOfRowsInSection一样:

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

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

1 个答案:

答案 0 :(得分:0)

这只是一个猜测,但是我认为您不应该在此处附加 filteredData

self.filteredData.append(self.myStories)
self.filteredData.append(self.filteredGroups  )
self.filteredData.append(self.filteredUsers)

这将使filteredData越来越长,而cellForRowAt仅使用前三个项目。您应该改为用三个子数组替换整个数组:

filteredData = [myStories, filteredGroups, filteredUsers]

我注意到的另一件事是,您要在上面的三行之后重新加载收藏视图,但是搜索栏似乎已安装在表视图上。也许您应该改为重新加载表格视图,否则这仅仅是一个错字。