如何将对象数组放在theArray中以便在tableView中显示结果?由于查询是异步的,我不能简单地说theArray = objects。那么,我如何从查询中截取数组呢?
编辑:
Ashish的回答解决了这个问题,但由于Xcode的建议,我不得不改变代码。
现在,我试图打开它,因为我需要,
var new = object.objectId
println("first: \(new)")
if let tryToUnwrap = new as? NSString {
println("second: \(tryToUnwrap)")
}
我downCast到NSString因为在Swift中字符串不是警告:从String转换?不相关的类型' NSString'总是失败
代码有效,但它意味着什么?我怎么能摆脱这个?
import UIKit
import Parse
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var myTableView: UITableView!
var theArray : [PFObject] = []
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//MARK: PFQUERY
//***********************************************************************
var query = PFQuery(className:"TestObject")
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
println("Successfully retrieved \(objects!.count) scores.")
// Do something with the found objects
self.theArray = (objects as? [PFObject])!
if self.theArray.count > 0 {
self.myTableView.reloadData()
}
if let objects = objects as? [PFObject] {
for object in objects {
// var new = object.objectId
// println("first: \(new)")
//
// if let tryToUnwrap = new as? NSString {
// println("second: \(tryToUnwrap)")
// }
println("here the object id: \(object.objectId)")
}
}
} else {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
}
}
//***********************************************************************
// if let string = dict.objectForKey("SomeKey") as? String {
// // If I get here, I know that "SomeKey" is a valid key in the dictionary, I correctly
// // identified the type as String, and the value is now unwrapped and ready to use. In
// // this case "string" has the type "String".
// println(string)
// }
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//MARK: UITableViewDataSource (required to conform to UITableViewDataSource)
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return theArray.count
}
//MARK: UITableViewDataSource (required to conform to UITableViewDataSource)
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell : TaskCell = tableView.dequeueReusableCellWithIdentifier("myCell") as! TaskCell
cell.objectIdLabel.text = theArray[indexPath.row].objectId
return cell
}
//MARK: UITableViewDelegate (not required)
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println("section is: \(indexPath.section) row is: \(indexPath.row)")
}
}
答案 0 :(得分:2)
获取后只需重新加载表格。
var query = PFQuery(className:"TestObject")
// query.whereKey("playerName", equalTo:"Sean Plott")
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
println("Successfully retrieved \(objects!.count) scores.")
// Do something with the found objects
theArray = objects as? [PFObject]
if theArray.count > 0 {
myTableView.reloadData()
}
if let objects = objects as? [PFObject] {
for object in objects {
println(object.objectId)
}
}
} else {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
}
}
答案 1 :(得分:1)
Parse还拥有自己的viewcontroller PFQueryTableViewController,其中包含一个查询,然后在表视图中设置单元格。我认为这可能对您的代码有用。
有关详细信息,请参阅https://parse.com/docs/ios/guide#user-interface-pfquerytableviewcontroller。
答案 2 :(得分:1)
为了您的编辑,您需要代码:
var new = object.objectId
println("first: \(new)")
if let tryToUnwrap:String = new {
println("second: \(tryToUnwrap)")
}