我试图查询我的Parse数据库,以便搜索用户名并显示与用户名相关的所有帖子。帖子分为两个字段,我贴上了#34;他们在功能里面一起回来了。
func searchDataBase() -> ([String]) {
if let username = PFUser.currentUser()?.username {
userHeading.text = username + "'s Profile"
var query = PFQuery(className:"Wish")
query.whereKey("user", equalTo:username)
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
println("Successfully retrieved \(objects!.count) wishes.")
// Do something with the found objects
if let objects = objects as? [PFObject] {
for object in objects {
//println(object.objectId)
var parseID = object.objectId!
var query = PFQuery(className:"Wish")
query.getObjectInBackgroundWithId(parseID) {
(detailedWish: PFObject?, error: NSError?) -> Void in
if error == nil {
let partOne = detailedWish!["wishFieldOne"] as! String
let partTwo = detailedWish!["wishFieldTwo"] as! String
let fullWish = "I wish I " + partOne + " when I " + partTwo
self.wishes.append(fullWish)
} else {
println(error)
}
}
}
}
} else {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
}
}
} else {
println("No user logged in")
}
return wishes
}
在上述功能运行后,我想将用户的帖子放在UITableView
内:
//TABLE VIEW FUNCTIONS
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
searchDataBase()
return wishes.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = recentPosts.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as! UITableViewCell
searchDataBase()
let row = indexPath.row
cell.textLabel?.text = wishes[row]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
recentPosts.deselectRowAtIndexPath(indexPath, animated: true)
searchDataBase()
let row = indexPath.row
println(wishes[row])
}
当我构建应用时,UITableView
不会显示我在searchDataBase
功能中查询的帖子。如果在追加数组后立即println(wishes)
,它就可以工作,但是当我在函数外打印数组时它是空的。 如何在查询成功后使wishes
数组的内容在函数外部可访问?
感谢您的帮助!
答案 0 :(得分:0)
WM_PAINT
执行返回数组。你正在访问的变量searchDataBase()
在哪里?
相反:
wishes
然后您将拥有表视图委托函数的数组。
答案 1 :(得分:0)
你不能做你想做的事情。
您需要做的是重写searchDataBase
方法以将完成闭包作为参数。然后在完成块中调用query.getObjectInBackgroundWithId
,构建结果数组并传递给searchDataBase
完成闭包。
答案 2 :(得分:0)
这是一种奇怪的做法。您在每个tableView方法上调用searchDataBase()。你肯定不想每次打电话解析?您的searchDataBase()方法也不需要返回数组。您只需将结果附加到此数组,并在完成后重新加载表。我假设这对你来说是一个更好的方法。
因此,我的解决方案是从所有tableView方法中删除searchDatabase()函数调用,并将其放在viewDidLoad()中。你也应该改变searchDataBase()看起来像这样
func searchDataBase(){
if let username = PFUser.currentUser()?.username {
userHeading.text = username + "'s Profile"
var query = PFQuery(className:"Wish")
query.whereKey("user", equalTo:username)
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
println("Successfully retrieved \(objects!.count) wishes.")
// Do something with the found objects
if let objects = objects as? [PFObject] {
for object in objects {
//println(object.objectId)
var parseID = object.objectId!
var query = PFQuery(className:"Wish")
query.getObjectInBackgroundWithId(parseID) {
(detailedWish: PFObject?, error: NSError?) -> Void in
if error == nil {
let partOne = detailedWish!["wishFieldOne"] as! String
let partTwo = detailedWish!["wishFieldTwo"] as! String
let fullWish = "I wish I " + partOne + " when I " + partTwo
self.wishes.append(fullWish)
} else {
println(error)
}
}
}
}
self.tableView.reloadData() // <- This will populate your tableView with your wishes
} else {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
}
}
} else {
println("No user logged in")
}
}