我在textfield中使用以下方法进行搜索,当我在表格视图中使用单个部分时,它正常工作。
但是当我在表格视图中有多个部分时,我不知道如何实现这个目标
在普通单段表格视图中
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个数组索引。但我需要它应该在他们的部分和部分名称应该是他们自己的,并更改过滤的字符串部分的索引部分
Say例如,如果搜索文本为 road ,则应为
R.S puram (Section 1)
- Lawly Road
- D.B Road
Town Hall (Section 2)
- Five corner road
我的tableview方法
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])"
}
我用它丢了我整整一天,请帮助解决这个问题....欢迎您的建议
答案 0 :(得分:2)
您可以在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
return searchResult.count
numberOfRowsInScetion
return searchResult[section].1.count
在titleForHeaderInSection方法中,您可以使用
return searchResult[section].0
在cellForRowAt方法
中 cell.textLabel?.text = "\(searchResult[indexPath.section].1[indexPath.row])"