【问题标题】:swift tableview with sections cannot use searchbar带有部分的快速表格视图无法使用搜索栏
【发布时间】:2019-10-29 16:42:01
【问题描述】:

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

    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

    }


}

我的 numbersOfRowsInSection 是这样的:

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

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

【问题讨论】:

    标签: ios swift uitableview


    【解决方案1】:

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

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

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

    filteredData = [myStories, filteredGroups, filteredUsers]
    

    我注意到的另一件事是,您在上述三行之后重新加载了 collection view,但搜索栏似乎安装在 table view 上。也许您应该重新加载表格视图,或者这只是一个错字。

    【讨论】:

    • 非常感谢。你能解释一下区别吗
    • @Silas 您最初的方法是“将这三个数组添加到嵌套数组中”,而我将其更改为“将嵌套数组的内容替换为仅三个数组”。使用您的方法,filteredData 会随着您不断添加内容而任意增长。
    猜你喜欢
    • 2017-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多