我想查询使用解析将字符串添加到数组。然后我想把这些字符串放到我的UITableView的单元格中。但是,每次运行应用程序时,似乎都没有出现在我的桌面上。这里是我的代码,如果有人可以帮助解释一些原因,它可能不会出现UITableView Swift问题
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var friendsArray: [String] = []
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
var usrname = currentUser?.username
var query = PFQuery(className:"Relation")
query.whereKey("Sender", equalTo : usrname!)
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
//println("Successfully retrieved \(objects) scores.")
// Do something with the found objects
if let objects = objects as? [PFObject] {
for object in objects {
var friendName = object["Friend"] as! String
println(friendName)
self.friendsArray.append(friendName)
}
}
}
else {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
}
}
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return friendsArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as! UITableViewCell
cell.textLabel?.text = self.friendsArray[indexPath.row]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}
那么我能做些什么来解决这个问题? – Justin
与昨天的同一问题类似。他们在异地使用异步代码。 http://stackoverflow.com/questions/30361517/asynchronous-tableviewcell-data/30361658#30361658。原则和解决方案选项相似。 – Gruntcakes
我很困惑,我可以确保Im在ViewdidLoad之后运行CellforRowAtIndexPath函数,因为我认为ViewDidLoad或者甚至是ViewDidAppear都会先发生 – Justin