问题描述
在User类中作为字符串变量.使用此变量作为PFQuery的键,并使用它来筛选出没有该变量的用户.我需要跳过没有该变量的用户,并将有该行为的用户添加到VC显示中.到目前为止,这是我的代码:在存储变量的VC中,我有:
Variable in a User class as a string. Using this variable as a key for a PFQuery and use it to filter out the users that don't have it.I need to skip users that don't have it and add the users that do to the VC display. Here is my code so far:In the VC where the variable is stored I have:
var genreSearch: NSString?
在单元格中的viewDidLoad之后,
after viewDidLoad in a cell I have:
let genreSearch = table_data[indexPath.row]
userDefaults.setObject(genreSearch, forKey: "genre")
print(genreSearch)`
然后在调用查询的VC上:
Then on the VC where the query is being called I have:
if let genreSearch = userDefaults.objectForKey("genre") as? [String] {
// do something here when a genresearch exists
let genreQuery = PFUser.query()
genreQuery!.whereKey("genre", containedIn: (genreSearch as [String]))
genreQuery!.findObjectsInBackgroundWithBlock { (users: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
// success
for user in users! {
self.appUsers.append(user as! PFUser)
} // users
self.resultsPageTableView.reloadData()
} // error
else {
}
}
}
任何帮助将不胜感激,我被困在此genreSearch查询中,感觉就像是几周!
Any help would be greatly appreciated I have been stuck on this genreSearch query for what feels like weeks!!!
这也是我在VC2上的代码,用于更新单元格中的用户信息:
Also here is my code on VC2 that is updating the users info in a cell:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let singleCell: CustomCell = tableView.dequeueReusableCellWithIdentifier("mySingleCellid") as! CustomCell
let userObject = appUsers[indexPath.row] as PFObject
singleCell.userName.text = userObject["name"] as? String
所以我已经找到了被称为的错误,所以我已经编辑了我的代码.*我能够打开VC,但是它不能过滤出结果?它显示所有用户,而不是跳过尚未选择流派的用户.有任何想法吗??? *
So I have edited my code as I figured out the errors that were being called *I am able to open the VC but it does not filter out the results? It displays all the users instead of skipping the users that haven't got the chosen genre. Any ideas??? *
推荐答案
最后弄清楚了为什么它没有过滤掉我的用户!
Finally figured out why it was not filtering out my users!!!
在存储变量的VC上,我在类上方缺少一条重要的代码:
On the VC where the variable was stored I was missing a vital line above the class :
var genreSearch: String?
let userDefaults = NSUserDefaults.standardUserDefaults() //this one
然后在我添加的ResultsVC中:
Then in the ResultsVC I added:
if let genre = userDefaults.stringForKey("genre") //this line
{
print(genre)
let genreQuery = PFUser.query()
genreQuery?.whereKey("username",notEqualTo:PFUser.currentUser()!.username!)
genreQuery!.whereKey("genre", equalTo: genre)
genreQuery!.findObjectsInBackgroundWithBlock { (genres: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
// success
self.appUsers.removeAll()
for genre in genres! {
self.appUsers.append(genre as! PFUser)
} // users
}
self.resultsPageTableView.reloadData()
self.refresher.endRefreshing()
} // error
我认为这不是在VC1上使用userDefaults保存或做任何事情,我一定错过了一步!!!
I think it just wasn't saving or doing anything with the userDefaults on VC1 I must have missed a step!!!
这篇关于使用变量作为PFQuery的键来筛选出用户.快速查询用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!