如何解决使用Realm快速进行数据过滤
在我的应用中,我正在使用jSONData
并存储到Realm.In
中。我使用UISegmentedControl
来显示三种类型的数据。像:All,Completed and InCompelted
。 App还具有一项功能,可以从UITableView中搜索数据。
//MARK: - SegmentButtonPressed
@IBAction func segmentPressed(_ sender: UISegmentedControl) {
let segmentTag = sender.selectedSegmentIndex
switch segmentTag {
case 0:
self.loadTodoList()
print(1)
case 1:
print(3)
todoLists = todoLists?.sorted(byKeyPath: "completed",ascending: false)
todoListTable.reloadData()
case 2:
todoLists = todoLists?.sorted(byKeyPath: "completed",ascending: true)
todoListTable.reloadData()
print(2)
default:
print("none")
}
}
UISearchBarDelegate
extension ViewController: UISearchBarDelegate {
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
todoLists = todoLists?.filter("title BEGINSWITH [cd] %@",searchBar.text!).sorted(byKeyPath: "title",ascending: true)
todoListTable.reloadData()
}
func searchBar(_ searchBar: UISearchBar,textDidChange searchText: String) {
if searchBar.text?.count == 0{
loadTodoList()
DispatchQueue.main.async {
searchBar.resignFirstResponder()
}
}
}
}
当用户按下其中任意一个UISegmentedControl
时,我就能过滤数据。但是在选择All,Completed and InCompelted
中的任何一个之后,如何使用UISearchBar
搜索数据并显示我的UiTableViewCell?预先感谢。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。