【发布时间】:2015-12-28 17:47:37
【问题描述】:
我使用 Realm 作为我的后端数据存储,并且我有一个带有 UISearchResults 控制器的 tableview。 如果我不过滤结果,我可以从 tableView 中删除一行并将其从我的 Realm 数据库中删除。 但是,当我使用 SearchController 过滤并尝试滑动删除该行时,我得到了错误。
原因:'无效更新:第 0 节中的行数无效。 更新后现有部分中包含的行数 (2) 必须等于该节之前包含的行数 更新(2),加上或减去插入或删除的行数 从该部分(0 插入,1 删除)和加号或减号 移入或移出该部分的行数(0 移入,0 移出)。'
这是我在过滤位置时使用的函数
func filterContentForSearchText(searchText: String, scope: String = "All") {
filteredLocations = locations.filter { location in
return location.site.lowercaseString.containsString(searchText.lowercaseString)
}
tableView.reloadData()
}
这是我的 tableView 删除功能
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
if searchController.active && searchController.searchBar.text != "" {
// This is from a filtered list
do {
try realm.write() {
self.realm.delete(filteredLocations[indexPath.row])
}
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
} catch {
print("Could not delete site")
}
} else {
do {
try realm.write() {
self.realm.delete(locations[indexPath.row])
}
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
} catch {
print("Could not delete site")
}
}
}
}
谁能帮我解决这个问题?
【问题讨论】:
标签: swift uitableview realm uisearchcontroller