我有一个"添加用户"按钮点击按钮设置"状态"反对"待定"。当用户具有此值时,我想更新我的按钮文本以说明"待定"。使用我当前的解决方案,文本会自动将每个按钮更改为"待定"尽管我有严格的条件,只有在查询匹配"待定"时才更改按钮。我的逻辑改变了我的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()
}
}
答案 0 :(得分:0)
正在发生的事情是查询要求Follow表中的任何对象的状态属性== "Pending"
。如果该表中的一个或多个对象满足该条件,那么" Pending"标题将应用于所有细胞'的按钮。
然后,代码中有两件事需要解决。首先,将Follow查询细化为关于当前行,可能与跟随对象中的指针属性等于userArray[row]
有关。
其次,如果用户在查询运行时滚动表格,则直接在查询的完成块中引用cell
会产生不良影响。该单元最终将被重用以表示不同的行。我的做法是在某个地方缓存查询的结果(比如在用户数组中)并在indexPath处重新加载行。