Uitableview 使用swift3在tableview中搜索栏和部分
我的数据库中有带有字母部分的tableview,我想添加搜索栏,但我不知道如何过滤数据并在tableview中实现它。 我的数据库存储在两个结构中: 一个包含所有数据的结构 第二个结构获取节的第一个字母,第一个结构作为数组 我的结构:Uitableview 使用swift3在tableview中搜索栏和部分,uitableview,swift3,uisearchbar,Uitableview,Swift3,Uisearchbar,我的数据库中有带有字母部分的tableview,我想添加搜索栏,但我不知道如何过滤数据并在tableview中实现它。 我的数据库存储在两个结构中: 一个包含所有数据的结构 第二个结构获取节的第一个字母,第一个结构作为数组 我的结构: struct SentenceInfo { // First struct (holds all the data) let name: String let detail: String let sentence: String
struct SentenceInfo { // First struct (holds all the data)
let name: String
let detail: String
let sentence: String
init(name: String, detail: String, sentence: String) {
self.name = name
self.detail = detail
self.sentence = sentence
}
}
struct SentenceNameSection { // Second struct (first letter and array of the first struct)
var firstLetter: String
var crimes: [SentenceInfo]
init(title: String, objects: [SentenceInfo]) {
firstLetter = title
crimes = objects
}
}
我的桌面视图:
var sections : [SentenceNameSection]!
var crimeData = [SentenceNameSection]()
var filteredData = [SentenceNameSection]()
var shouldShowSearchResults = false
var searchController: UISearchController!
func updateSearchResults(for searchController: UISearchController) {
let searchString = searchController.searchBar.text
filteredData = crimeData.filter({ (crime) -> Bool in
let crimeMatch: String = crime // Error about types
return ((crimeMatch.range(of: searchString!) != nil))
})
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: sentenceTableViewCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifer, for: indexPath) as! sentenceTableViewCell
let crime: SentenceInfo = sections[indexPath.section].crimes[indexPath.row]
cell.nameLabel.text = crime.name
cell.detailLabel.text = crime.detail
cell.sentenceLabel.text = crime.sentence
return cell
}
首先,
crimeData
包含的sentencenamesesection
无法与String
除此之外,要过滤数据源数组(包括节),您必须使用重复循环并创建新的SentenceNameSection
项
此代码在SentenceInfo
struct中搜索所有三个属性
let searchString = searchController.searchBar.text!
filteredData.removeAll() // is mandatory to empty the filtered array
for section in crimeData {
let filteredContent = section.crimes.filter { $0.name.range(of: searchString) != nil
|| $0.detail.range(of: searchString) != nil
|| $0.sentence.range(of: searchString) != nil
}
if !filteredContent.isEmpty {
filteredData.append(SentenceNameSection(title: section.firstLetter, objects: filteredContent))
}
}
注意:当然,您必须在所有适当的表视图数据源和委托方法中处理
搜索
案例。谢谢您,它成功了。为什么当我尝试将其实现到表视图时,会出现错误(“致命错误:索引超出范围”)代码:('let filteredCrime:SentenceInfo=filteredData[indexath.section].crimes[indexath.row]'))确保从numberOfSections
和numberOfRows
返回正确的值,具体取决于注释中提到的搜索控制器的isActive
。您没有注意到这一点。工作很好。谢谢你的帮助
For Swift 3 , below is the sample code
Struct BookDetails{
var title:String?
var author:String?
}
var filteredSearch:[BookDetails] = []
filteredSearch = self.bookDetails.filter { (data) -> Bool in
return data.title?.range(of: searchText, options: String.CompareOptions.caseInsensitive) != nil || data.author?.range(of: searchText, options: String.CompareOptions.caseInsensitive) != nil
}