无法使用 whereKey 为 textDidChange 搜索两个字符串对象
Can't search two string objects with whereKey for textDidChange
我有一个 tableView
,我正在其中从 Parse 加载我的数据。现在我想使用 parse 来显示来自 searchBar.text 的搜索结果。我有名字和姓氏,我都想在其中搜索。问题是我不知道如何在 Class 中查询搜索 2 个对象并在 [=11= 中显示过滤后的匹配项].它完全适用于一个对象。
我尝试了 2 个 whereKey 方法但都失败了,它只会显示对象的两个首字母开头的单元格;示例:约翰·约翰森
希望我把问题说清楚了。
更新二:
class TableViewController: UITableViewController, UISearchBarDelegate {
@IBOutlet var searchBar: UISearchBar! = UISearchBar()
var timeLineData:NSMutableArray = NSMutableArray()
override func viewDidLoad() {
super.viewDidLoad()
searchBar.delegate = self
....
}
func loadData(name:String){
timeLineData.removeAllObjects()
var firstName = PFQuery(className: "Newfamilie")
firstName.whereKey("voornaam", containsString: name)
var lastName = PFQuery(className: "Newfamilie")
lastName.whereKey("achternaam", containsString: name)
var query = PFQuery.orQueryWithSubqueries([firstName, lastName])
query.orderByAscending("achternaam")
query.limit = 200
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]!, error: NSError!) -> Void in
if error == nil {
for object in objects{
let naam:PFObject = object as PFObject
self.timeLineData.addObject(naam)
}
self.tableView.reloadData()
self.refreshTimeLineControl.endRefreshing()
println("\(self.timeLineData.count)")
}
}
}
override func viewDidAppear(animated: Bool) {
self.loadData("")
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return timeLineData.count
}
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
loadData(searchText)
}
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
loadData("")
}
override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell {
let cell = tableView!.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath!) as UITableViewCell
let persoon:PFObject = self.timeLineData.objectAtIndex(indexPath!.row) as PFObject
let voornaam = persoon.objectForKey("voornaam") as String
let achternaam = persoon.objectForKey("achternaam") as String
cell.textLabel.text = "\(voornaam) \(achternaam)"
return cell
}
问题是您搜索的要求是名字和姓氏必须包含要获取的字符串结果。您需要组合两个 PFQuery
-实例 - 这将为您提供 voornaam
OR achternaam
的结果,而不是 AND.
var firstName = PFQuery(className: "Newfamilie")
firstName.limit = 200
firstName.orderByAscending("achternaam")
firstName.whereKey("voornaam", containsString: name)
var lastName = PFQuery(className: "Newfamilie")
lastName.limit = 200
lastName.orderByAscending("achternaam")
lastName.whereKey("achternaam", containsString: name)
var query = PFQuery.orQueryWithSubqueries([firstName, lastName])
query.findObjectsInBackgroundWithBlock {
(results: [AnyObject]!, error: NSError!) -> Void in
if error == nil {
// Results will be data containing the string "name" in "voornaam" OR "achternaam"
for object in objects{
let naam:PFObject = object as PFObject
self.timeLineData.addObject(naam)
}
self.tableView.reloadData()
println("\(self.timeLineData.count)")
}
}
我有一个 tableView
,我正在其中从 Parse 加载我的数据。现在我想使用 parse 来显示来自 searchBar.text 的搜索结果。我有名字和姓氏,我都想在其中搜索。问题是我不知道如何在 Class 中查询搜索 2 个对象并在 [=11= 中显示过滤后的匹配项].它完全适用于一个对象。
我尝试了 2 个 whereKey 方法但都失败了,它只会显示对象的两个首字母开头的单元格;示例:约翰·约翰森
希望我把问题说清楚了。
更新二:
class TableViewController: UITableViewController, UISearchBarDelegate {
@IBOutlet var searchBar: UISearchBar! = UISearchBar()
var timeLineData:NSMutableArray = NSMutableArray()
override func viewDidLoad() {
super.viewDidLoad()
searchBar.delegate = self
....
}
func loadData(name:String){
timeLineData.removeAllObjects()
var firstName = PFQuery(className: "Newfamilie")
firstName.whereKey("voornaam", containsString: name)
var lastName = PFQuery(className: "Newfamilie")
lastName.whereKey("achternaam", containsString: name)
var query = PFQuery.orQueryWithSubqueries([firstName, lastName])
query.orderByAscending("achternaam")
query.limit = 200
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]!, error: NSError!) -> Void in
if error == nil {
for object in objects{
let naam:PFObject = object as PFObject
self.timeLineData.addObject(naam)
}
self.tableView.reloadData()
self.refreshTimeLineControl.endRefreshing()
println("\(self.timeLineData.count)")
}
}
}
override func viewDidAppear(animated: Bool) {
self.loadData("")
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return timeLineData.count
}
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
loadData(searchText)
}
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
loadData("")
}
override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell {
let cell = tableView!.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath!) as UITableViewCell
let persoon:PFObject = self.timeLineData.objectAtIndex(indexPath!.row) as PFObject
let voornaam = persoon.objectForKey("voornaam") as String
let achternaam = persoon.objectForKey("achternaam") as String
cell.textLabel.text = "\(voornaam) \(achternaam)"
return cell
}
问题是您搜索的要求是名字和姓氏必须包含要获取的字符串结果。您需要组合两个 PFQuery
-实例 - 这将为您提供 voornaam
OR achternaam
的结果,而不是 AND.
var firstName = PFQuery(className: "Newfamilie")
firstName.limit = 200
firstName.orderByAscending("achternaam")
firstName.whereKey("voornaam", containsString: name)
var lastName = PFQuery(className: "Newfamilie")
lastName.limit = 200
lastName.orderByAscending("achternaam")
lastName.whereKey("achternaam", containsString: name)
var query = PFQuery.orQueryWithSubqueries([firstName, lastName])
query.findObjectsInBackgroundWithBlock {
(results: [AnyObject]!, error: NSError!) -> Void in
if error == nil {
// Results will be data containing the string "name" in "voornaam" OR "achternaam"
for object in objects{
let naam:PFObject = object as PFObject
self.timeLineData.addObject(naam)
}
self.tableView.reloadData()
println("\(self.timeLineData.count)")
}
}