Tableview列表是空的,因为findObjectInBackgroundWithBlock返回nil

时间:2015-06-06 18:46:53

标签: ios uitableview swift parse-platform pfobject

这些是我在swift中的代码 这是用解析

检索当前用户的好友列表对象的代码
class UserViewController: UITableViewController {

var userArray: NSMutableArray = []

@IBOutlet weak var friendListTableView: UITableView!
override func viewDidLoad() {
    super.viewDidLoad()

       retrieveMessages()

   }
func retrieveMessages() {
    var query = PFUser.query()
    if let username = PFUser.currentUser().username {
        query.whereKey("username", equalTo: username)
        query.findObjectsInBackgroundWithBlock {
        (objects, error) -> Void in

        for object in objects! {
            let usernames:String? = (object as PFObject)["Friends"] as? String
            println(usernames) // It prints "nil"
            if usernames != nil {
                self.userArray.addObject(usernames!)
            }
        }
                dispatch_async(dispatch_get_main_queue()) {
                    self.friendListTableView.reloadData()
                }

    }

}
}
            override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.
    return userArray.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    // Update - replace as with as!

    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

    cell.textLabel?.text = userArray[indexPath.row] as? String

    return cell
}

这是当前用户通过用户名解析时添加当前用户朋友的代码

class addUserViewController: UIViewController {

@IBOutlet weak var usernameTextField: UITextField!

@IBAction func addUser(sender: AnyObject) {
    var query = PFUser.query()
    query.whereKey("username", equalTo: usernameTextField.text)
    println("Pass")
    query.getFirstObjectInBackgroundWithBlock{ (object:PFObject!, error: NSError!) -> Void in

        if error == nil {
            let currentUser = PFUser.currentUser()
            let friendList = currentUser.relationForKey("Friends")

            var addFriend = object
            if addFriend != nil {
                friendList.addObject(addFriend)
                println("added")
            }
            PFUser.currentUser().saveInBackgroundWithBlock{
                (succeeded: Bool!, error: NSError!) in

                if error != nil {
                    println("Error")
                }
                else {
                    println("saved")
                }
            }
        }

    }
}

我想要检索当前用户的好友列表以在tableview中显示它,但是tableview不会更新以显示当前用户的好友列表,它是空的,并且那里有&#39 ;在tableview中根本没有用户列表。我试图修复它并检查方法是否获得任何对象。

问题是当我使用println(用户名)时,它会打印" nil"该方法根本没有得到任何对象。首先我使用用户名作为NSArray然后我将其更改为NSMutableArray并且它没有像NSArray那样的附加方法,所以我做了一个研究并添加"添加对象"它中的代码行并改变了一些东西。现在我完全不确定我的代码有什么问题,而且我已经被困在这一周了一段时间,如果我的代码有问题,请指教我哪里?或修理它会很棒。任何帮助表示赞赏

这是我的用户在解析中的班级表的屏幕截图 https://www.dropbox.com/s/6xp48v3yn0l2hje/Screen%20Shot%202015-06-03%20at%202.10.13%20PM.png?dl=0

这里是我当前用户在解析中的朋友的屏幕截图,使用PFRelation方法保存,如上所示 https://www.dropbox.com/s/pd8mt8sf35u1m0v/Screen%20Shot%202015-06-03%20at%202.10.55%20PM.png?dl=0

提前致谢!!

0 个答案:

没有答案