UISearchBar iOS 返回索引超出范围的错误
UISearchBar iOS returning error of index out of range
我花了大约 3 个小时试图让这个 UISearchbar 工作。我有一个 Notes
的自定义 class,它有一个 .name
的 属性。我正在尝试通过 属性 .name
过滤数据(一组注释)。我已经实现了 delegate
并实例化了第二个过滤数组以及所有必需的协议要求。但是,我似乎无法让我的搜索功能获得 return 正确的数据。以下是我正在使用的当前代码。它构建良好,但是当我开始在搜索栏中输入时,我收到 index out range
错误,这发生在下面的行中并附有注释。这是在视图控制器和其中的 table 控制器内完成的。
var notes = [Notes]()
var searchActive : Bool = false
var filtered = [Notes]()
/// these above vars are global ///
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
searchActive = true;
}
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
searchActive = false;
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searchActive = false;
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchActive = false;
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
filtered = notes.filter(){ (Notes) -> Bool in
let range = Notes.name.range(of: searchText, options: NSString.CompareOptions.caseInsensitive)
return range != nil
}
if(filtered.count == 0){
searchActive = false;
} else {
searchActive = true;
}
self.tableView.reloadData()
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var note: Notes
if(searchActive){
//// THIS IS WHERE THE ERROR IS THROWN /////
note = filtered[indexPath.row]
} else {
note = notes[indexPath.row]
}
if let cell = tableView.dequeueReusableCell(withIdentifier: "NoteCell", for: indexPath as IndexPath) as? NoteCell {
cell.configureCell(note: note)
return cell
} else {
return NoteCell()
}
}
在方法 searchBarTextDidEndEditing
、searchBarCancelButtonClicked
和 searchBarSearchButtonClicked
中将 searchActive
设置为 false
后,也尝试重新加载 tableView
。
注意:不要在方法 searchBarTextDidBeginEditing
中将 searchActive
设置为 true
因为如果你在 searbar 上贴了胶带但没有输入任何内容并尝试滚动然后你也会得到索引越界崩溃。
编辑: 检查 numberOfRowsInSection
是否正确执行。
func tableView(_ tableView:UITableView, numberOfRowsInSection section:Int) -> Int {
if searchActive {
return filtered.count
}
return notes.count
}
我花了大约 3 个小时试图让这个 UISearchbar 工作。我有一个 Notes
的自定义 class,它有一个 .name
的 属性。我正在尝试通过 属性 .name
过滤数据(一组注释)。我已经实现了 delegate
并实例化了第二个过滤数组以及所有必需的协议要求。但是,我似乎无法让我的搜索功能获得 return 正确的数据。以下是我正在使用的当前代码。它构建良好,但是当我开始在搜索栏中输入时,我收到 index out range
错误,这发生在下面的行中并附有注释。这是在视图控制器和其中的 table 控制器内完成的。
var notes = [Notes]()
var searchActive : Bool = false
var filtered = [Notes]()
/// these above vars are global ///
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
searchActive = true;
}
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
searchActive = false;
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searchActive = false;
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchActive = false;
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
filtered = notes.filter(){ (Notes) -> Bool in
let range = Notes.name.range(of: searchText, options: NSString.CompareOptions.caseInsensitive)
return range != nil
}
if(filtered.count == 0){
searchActive = false;
} else {
searchActive = true;
}
self.tableView.reloadData()
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var note: Notes
if(searchActive){
//// THIS IS WHERE THE ERROR IS THROWN /////
note = filtered[indexPath.row]
} else {
note = notes[indexPath.row]
}
if let cell = tableView.dequeueReusableCell(withIdentifier: "NoteCell", for: indexPath as IndexPath) as? NoteCell {
cell.configureCell(note: note)
return cell
} else {
return NoteCell()
}
}
在方法 searchBarTextDidEndEditing
、searchBarCancelButtonClicked
和 searchBarSearchButtonClicked
中将 searchActive
设置为 false
后,也尝试重新加载 tableView
。
注意:不要在方法 searchBarTextDidBeginEditing
中将 searchActive
设置为 true
因为如果你在 searbar 上贴了胶带但没有输入任何内容并尝试滚动然后你也会得到索引越界崩溃。
编辑: 检查 numberOfRowsInSection
是否正确执行。
func tableView(_ tableView:UITableView, numberOfRowsInSection section:Int) -> Int {
if searchActive {
return filtered.count
}
return notes.count
}