【发布时间】:2019-01-01 03:45:01
【问题描述】:
我有 2 个数组,一个是 base 包含 532 个元素,另一个是 filter 数组。我有UISearchBar,它有委托方法
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
self.filter(searchText: searchText)
}
private func filter(searchText: String) {
if searchText.isEmpty {
self.filterDelegates = self.delegates
} else {
self.filterDelegates = self.delegates.filter {
$0.delegate.fullName.lowercased().contains(searchText.lowercased())
}
}
self.tableView.reloadData()
}
当我滚动 tableview 并开始搜索时它崩溃了,它没有在 self.tableView.reloadData() 方法上重置 indexPath
在索引路径的 CellforRow 中登录
INDEXPATH : 23
FILTER COUNT : 396
INDEXPATH : 22
FILTER COUNT : 20
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(
withIdentifier: DelegateTableViewCell.defaultReuseIdentifier
) as! DelegateTableViewCell
print("INDEXPATH : \(indexPath.row)")
print("FILTER COUNT : \(self.filterDelegates.count)")
cell.item = self.filterDelegates[indexPath.row] // Index out of bound
return cell
}
我也使用相同的过滤器数组。行数法
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.filterDelegates.count
}
我做错了什么?
【问题讨论】:
-
发布
UITableViewDataSource函数numberOfRows的内容 -
FILTER COUNT : 20,INDEXPATH : 22,return self.filterDelegates.count我想这是你的问题 -
检查索引
-
@PrashantTukadiya 是一种解决方法,可以解决真正的问题。
-
不知道它是否会工作,但你可以把它放在开始更新和结束更新块中
标签: ios swift uitableview uisearchbar