我用搜索栏创建了一个表格 View 。我的数据集如下所示:
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 总是完整的。
最佳答案
这只是一个猜测,但我认为你不应该追加到 filteredData
这里:
self.filteredData.append(self.myStories)
self.filteredData.append(self.filteredGroups )
self.filteredData.append(self.filteredUsers)
这将使
filteredData
越来越长,cellForRowAt
只使用前三个项目。您应该用三个子数组替换整个数组:filteredData = [myStories, filteredGroups, filteredUsers]
我注意到的另一件事是您在上面三行之后重新加载了一个 Collection View ,但搜索栏似乎安装在一个表格 View 上。也许您应该重新加载表格 View ,或者这只是一个错字。
关于ios - 带有部分的快速表格 View 无法使用搜索栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58611880/