Swift 根据解析查询更改按钮状态

Swift Changing Button State Based on Parse Query

我有一个 "Add User" 按钮,单击该按钮会将 "status" 对象设置为 "Pending"。当用户有这个值时,我想更新我的按钮文本说 "Pending"。使用我当前的解决方案,文本会自动将每个按钮更改为 "Pending",尽管我的严格条件是仅当查询匹配 "Pending" 时才更改按钮。我更改 tableview 中所有按钮的逻辑有什么问题?

按钮状态逻辑:

var friendshipStatusQuery = PFQuery(className: "Follow")

        friendshipStatusQuery.whereKey("status", equalTo: "Pending")

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

                cell.addUserButton.setTitle("Pending", forState: UIControlState.Normal)

            } else {
                NSLog("Error: %@ %@", error, error.userInfo!)
            }
        }

完整代码:

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)


        var friendshipStatusQuery = PFQuery(className: "Follow")

        friendshipStatusQuery.whereKey("status", equalTo: "Pending")

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

                cell.addUserButton.setTitle("Pending", forState: UIControlState.Normal)

            } else {
                NSLog("Error: %@ %@", error, error.userInfo!)
            }
        }


        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()


    }

}

发生的事情是查询在关注 table 谁的状态属性 == "Pending" 中询问 any object。如果 table 中的一个或多个 object 满足该条件,则 "Pending" 标题将应用于所有单元格的按钮。

那么,代码中有两件事需要修复。首先,将 Follow 查询细化为关于当前行,可能与 follow object 中的指针属性等于 userArray[row].

有关

其次,如果用户在查询为 运行 时滚动 table,直接在查询的完成块中引用 cell 将产生不良影响。该单元格最终将被重用以表示不同的行。我的做法是将查询结果缓存在某个地方(比如在用户数组中)并在 indexPath 重新加载行。