我试图在tableView中实现searchBar,但是我收到了这个错误。我无法弄清楚如何删除它。
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
searchingDataArray = origanalArray.filter({ (text) -> Bool in
let tmp: String = text
let range = tmp.range(of: searchText, options: NSString.CompareOptions.caseInsensitive)
return range.location != NSNotFound
})
if (searchingDataArray.count == 0) {
searching = false
} else {
searching = true
}
self.tableView.reloadData()
}
此错误出现在代码的第5行,它听起来像这样:类型范围的值string.index没有成员位置
答案 0 :(得分:2)
针对Swift 3进行了更新:
使用代码行;
// MARK:- Varirabl Declartion
@IBOutlet weak var mSearchBar: UISearchBar!
@IBOutlet weak var mTableView: UITableView!
var isSearch : Bool = false
var arrCountry = ["Afghanistan", "Algeria", "Bahrain","Brazil", "Cuba", "Denmark","Denmark", "Georgia", "Hong Kong", "Iceland", "India", "Japan", "Kuwait", "Nepal"]
var arrFilter:[String] = []
// MARK:UISearchBar委托方法
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if searchText.characters.count == 0 {
isSearch = false;
self.mTableView.reloadData()
} else {
arrFilter = arrCountry.filter({ (text) -> Bool in
let tmp: NSString = text as NSString
let range = tmp.range(of: searchText, options: NSString.CompareOptions.caseInsensitive)
return range.location != NSNotFound
})
if(arrFilter.count == 0){
isSearch = false;
} else {
isSearch = true;
}
self.mTableView.reloadData()
}
}
答案 1 :(得分:0)
尝试这可能对您有所帮助:
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
filtered = data.filter({ (text) -> Bool in
let tmp: NSString = text
let range = tmp.rangeOfString(searchText, options: NSStringCompareOptions.CaseInsensitiveSearch)
return range.location != NSNotFound
})
if(filtered.count == 0){
searchActive = false;
} else {
searchActive = true;
}
self.tableView.reloadData()
}