使用parse在tableview中检索和显示用户的对象,但tableview不会更新

时间:2015-06-05 09:45:40

标签: ios uitableview swift parse-platform pfobject

这是我在swift中的代码

class UserViewController: UITableViewController {

var userArray: [String] = []

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

       retrieveMessages()
       self.friendListTableView.reloadData()


   }
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
            if usernames != nil {
                self.userArray.append(usernames!)
            }
        }

    }

}
}
            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]


    return cell
}

我的UITableView未获得更新。我已将self.friendListTableView.reloadData()的位置从retrieveMessage函数内部更改为viewDidLoad方法内部且表格仍然为空。我相信retrieveMessage()中的方法在某种程度上是不正确的,可能在for object in objects { ... }内。

这是我"用户"的截图。解析中的类 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

我们非常感谢您的帮助,如果您需要任何其他信息,请与我们联系。 谢谢

3 个答案:

答案 0 :(得分:0)

1)你是否在故事板中标记了你的UserViewController是dataSource并为tableView委托?我的意思是:dataSource and delegate for tableView

2)我想你在这里重新加载了tableView:

query.findObjectsInBackgroundWithBlock {
    (objects, error) -> Void in

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

}}

答案 1 :(得分:0)

在填充数组后,您似乎忘记在桌面上调用reloadData

之后:

for object in objects! {
   let usernames:String? = (object as PFObject)["Friends"] as? String
   if usernames != nil {
       self.userArray.append(usernames!)
   }
}

你需要这个:

dispatch_async(dispatch_get_main_queue()) {
    self.friendListTableView.reloadData()
}

答案 2 :(得分:0)

WebElement user = driver1.findElement(By.id("usrname")); user.sendKeys("username"); System.out.println("Username entered"); WebElement password = driver1.findElement(By.id("usrpwd")); password.sendKeys("password"); System.out.println("password entered"); WebElement submit = driver1.findElement(By.xpath(".//*[@id='ibm-pcon']/form/table[2]/tbody/tr/td/input[1]")); submit.click(); System.out.println("submit button entered"); Thread.sleep(7000); WebElement role = driver1.findElement(By.xpath("//input[@id='userRole'][@value='ARCOL '][@type='radio'][@name='role']")); role.click(); System.out.println("role entered"); 在后​​台运行(顾名思义)。来自documentation for PFQuery

  

异步查找对象并使用。调用给定的块   结果

因此,当您在findObjectsInBackgroundWithBlock中致电userArray时,friendListTableView.reloadData()将无法填充viewDidLoad。根据{{​​3}},您应该在retrieveMessages中执行以下操作:

//...

for object in objects! {
    // You can shorten the code in your for-loop to:
    if let usernames = (object as PFObject)["Friends"] as? String {
        self.userArray.append(usernames)
    }
}

// Here userArray has been updated so it's a good time to call reloadData().
// (You should always update UI on the main thread)
dispatch_async(dispatch_get_main_queue()) {
    friendListTableView.reloadData()
}

//...

如果您不熟悉dispatch_async,我建议您查看iTunesU上的How can I populate a UITableView with data from a parse.com query after it is done running in the background?,这是一个关于多线程的好文章。