我使用以下方法搜索文本字段,当我在表格 View 中使用单个部分时,它工作正常。
但是当我在 TableView 中有多个部分时,我不知道如何实现这一点
在普通单节表格 View 中
foodTypeArr = ["Japanesh", "Chinesh", "Lebanesh", "Italian", "Indian"]
searchResultArr.addObjects(from: foodTypeArr as! [Any])
func searchAutocompleteEntriesWithSubstring(substring: String)
{
searchResultArr.removeAllObjects()
for curString in foodTypeArr
{
let myString:NSString! = curString as! NSString
if (myString).lowercased.contains(substring)
{
searchResultArr.add(curString)
}
}
self.tblList.reloadData()
}
在多节 TableView 中
var searchAreaArr = NSMutableArray()
var areaArr = [[String]]()
var areaTitles = NSArray()
areaArr = [["Vadvalli", "Mullai Nagar", "P.N.Pudhur"], ["Lawly Road", "Kowly Brown", "D.B Road"], ["Raja Street", "Gandhipark", "Five corner road”, "Main Town Hall”]]
areaTitles = ["Vadavalli", "R.S.Puram", "Town Hall"]
for i in 0..<areaArr.count
{
searchAreaArr.addObjects(from: areaArr[i])
}
我在 TextField-shouldChangeCharactersIn 范围内调用以下方法
func searchAutocompleteEntriesWithSubstring(substring: String)
{
areaArr.removeAll()
var mArr = [String]()
for curString in searchAreaArr
{
let myString:NSString! = curString as! NSString
if (myString).lowercased.contains(substring)
{
mArr.append(curString as! String)
}
}
areaArr.insert(mArr, at: 0)
}
这样,它将过滤后的字符串插入 areaArr 中的 第 0 个数组索引。但我需要它应该在他们的部分并且部分名称应该是他们自己的,并更改字符串过滤部分的部分索引
比如说,如果搜索文本是road,它应该是
R.S puram (Section 1)
- Lawly Road
- D.B Road
Town Hall (Section 2)
- Five corner road
我的表格 View 方法
func numberOfSections(in tableView: UITableView) -> Int
{
return areaArr.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return areaArr[section].count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String?
{
if section < areaTitles.count
{
return areaTitles[section] as? String
}
return nil
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell:UITableViewCell =
self.tblList.dequeueReusableCell(withIdentifier: "cell") as UITableViewCell!
cell.textLabel?.text = "\(areaArr[indexPath.section][indexPath.row])"
}
我失去了我的一整天,请帮助解决这个问题......欢迎您提出建议
最佳答案
你可以在 shouldChangeCharactersIn 方法中做这样的事情
let areaArr = [["Vadvalli", "Mullai Nagar", "P.N.Pudhur"], ["Lawly Road", "Kowly Brown", "D.B Road"], ["Raja Street", "Gandhipark", "Five corner road", "Main Town Hall"]]
let areaTitles = ["Vadavalli", "R.S.Puram", "Town Hall"]
let searchText = "road"
var areaArrResult = areaArr.map { $0.filter({ $0.lowercased().contains(searchText) }) }
let areaTitlesResult = areaTitles.filter { !areaArrResult[areaTitles.index(of: $0)!].isEmpty }
areaArrResult = areaArrResult.filter { !$0.isEmpty }
print(areaArrResult)//[["Lawly Road", "D.B Road"], ["Five corner road"]]
print(areaTitlesResult)//["R.S.Puram", "Town Hall"]
或
var allDetails:[(String,[String])] = []
var searchResult:[(String,[String])] = []
allDetails = [("Vadavalli",["Vadvalli", "Mullai Nagar", "P.N.Pudhur"]),
("R.S.Puram",["Lawly Road", "Kowly Brown", "D.B Road"]),
("Town Hall",["Raja Street", "Gandhipark", "Five corner road", "Main Town Hall"])]
searchAreaArr = areaArr.map({ ($0.0,$0.1.filter({ $0.lowercased().contains(substring) })) }).filter { !$0.1.isEmpty}
print(searchResult)//[("R.S.Puram", ["Lawly Road", "D.B Road"]), ("Town Hall", ["Five corner road"])]
然后
在numberOfSections
返回searchResult.count
在numberOfRowsInScetion
返回searchResult[section].1.count
在 titleForHeaderInSection 方法中你可以使用
return searchResult[section].0
在 cellForRowAt 方法中
cell.textLabel?.text = "\(searchResult[indexPath.section].1[indexPath.row])"
关于swift - 在多部分 TableView 中使用 TextField 进行搜索 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44411778/