Swift - 两个条件的Parse查询不创建正确的结果

时间:2015-05-08 18:30:01

标签: ios uitableview swift parse-platform

我有一个有三个对象的类:

Class: Follow
Follow["follower"] = Current User
Follow["following"] = IndexPath of row for button click
Follow["status"] = Pending or Approved

我现在已经使用我的tableview控制器实现了功能,其中tableview将填充除当前用户之外的所有用户。将显示用户配置文件图像和用户名,以及“添加用户”按钮。单击添加用户按钮时,“跟随”类对象将设置为上面显示,状态设置为“待定”。此功能有效,但我想要做的是在为每个用户行单击按钮时将“添加用户”文本更新为“待定”。我目前在我的Parse查询中应用两个条件,“status”=“待定”和“跟随者”等于“当前用户的用户名”此技术似乎不起作用,因为单击按钮时文本不会更改。有什么帮助吗?

查询:

//** Query Pending Frienship Statuses and Change Button Title **//

        var friendshipStatusQuery = PFQuery(className: "Follow")

        friendshipStatusQuery.whereKey("status", equalTo: "Pending")
        friendshipStatusQuery.whereKey("follower", equalTo: PFUser.currentUser().username)


        friendshipStatusQuery.findObjectsInBackgroundWithBlock {
            (objects: [AnyObject]!, error: NSError!) -> Void in
            if error != nil {

                for object in objects {
                cell.addUserButton.setTitle("Pending", forState: UIControlState.Normal)
                }
            } else {
               println(error)
            }
        }

完整代码:

import UIKit

class SearchUsersRegistrationTableViewController: UITableViewController {

    var userArray : NSMutableArray = []

    override func viewDidLoad() {
        super.viewDidLoad()

        var user = PFUser.currentUser()

        loadParseData()




    }

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

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

        return 1

    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return userArray.count

    }

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

        let cell: SearchUsersRegistrationTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! SearchUsersRegistrationTableViewCell

        let row = indexPath.row

        var individualUser = userArray[row] as! PFUser
        var username = individualUser.username as String

        var profileImage = individualUser["profileImage"] as? PFFile

        if profileImage != nil {

        profileImage!.getDataInBackgroundWithBlock({
            (result, error) in

            cell.userImage.image = UIImage(data: result)

        })
        } else {
            cell.userImage.image = UIImage(named: "profileImagePlaceHolder")

        }

        cell.usernameLabel.text = username

        cell.addUserButton.tag = row

        cell.addUserButton.addTarget(self, action: "addUser:", forControlEvents: .TouchUpInside)

        //** Query Pending Frienship Statuses and Change Button Title **//

        var friendshipStatusQuery = PFQuery(className: "Follow")

        friendshipStatusQuery.whereKey("status", equalTo: "Pending")
        friendshipStatusQuery.whereKey("follower", equalTo: PFUser.currentUser().username)


        friendshipStatusQuery.findObjectsInBackgroundWithBlock {
            (objects: [AnyObject]!, error: NSError!) -> Void in
            if error != nil {

                for object in objects {
                cell.addUserButton.setTitle("Pending", forState: UIControlState.Normal)
                }
            } else {
               println(error)
            }
        }


        return cell

    }

    func loadParseData() {

        var user = PFUser.currentUser()

        var query : PFQuery = PFUser.query()

        query.whereKey("username", notEqualTo: user.username)


        query.findObjectsInBackgroundWithBlock {
            (objects:[AnyObject]!, error:NSError!) -> Void in

            if error == nil {

                if let objects = objects {

                    println("\(objects.count) users are listed")

                    for object in objects {


                        self.userArray.addObject(object)



                    }
                    self.tableView.reloadData()
                }
            } else {
                println("There is an error")
            }
        }
    }






    @IBAction func addUser(sender: UIButton) {

        println("Button Triggered")

        let addUserButton : UIButton = sender as UIButton!

        let user : PFObject = self.userArray.objectAtIndex(addUserButton.tag) as! PFObject



        var follow = PFObject(className: "Follow")



        follow["following"] = self.userArray.objectAtIndex(sender.tag)
        follow["follower"] = PFUser.currentUser().username
        follow["status"] = "Pending"


        follow.saveInBackground()


    }

}

1 个答案:

答案 0 :(得分:0)

如果单击某个按钮,则会调用addUser:方法,该方法在该方法中根本不会将标题更改为待处理。看起来它为Follow Class添加了一个新对象。

我对你在cellForRowAtIndexPath中的Parse查询感到有点困惑:

您查询待处理的友情,并且对于您找到的每个友情,您将当前正在创建状态的单元格更改为待处理多次。我认为您应该更改查询以仅使用当前单元格用户名评估友谊是否正在等待。您应该能够创建更快的查询,并且可能只使用countObjectsInBackgroundWithBlock,因为您似乎没有对从查询返回的对象执行任何操作。

如果我能提供帮助或提供澄清,请告诉我。我希望我能正确理解这个问题