Swift 基于解析查询快速改变按钮状态
我有一个“添加用户”按钮,点击该按钮后,将“状态”对象设置为“待定”。当用户有这个值时,我想更新我的按钮文本,说“待定”。在我当前的解决方案中,文本自动将每个按钮更改为“挂起”,尽管我严格要求仅当查询匹配“挂起”时才更改按钮。更改tableview中所有按钮的逻辑有什么问题 按钮状态逻辑:Swift 基于解析查询快速改变按钮状态,swift,parse-platform,pfquery,Swift,Parse Platform,Pfquery,我有一个“添加用户”按钮,点击该按钮后,将“状态”对象设置为“待定”。当用户有这个值时,我想更新我的按钮文本,说“待定”。在我当前的解决方案中,文本自动将每个按钮更改为“挂起”,尽管我严格要求仅当查询匹配“挂起”时才更改按钮。更改tableview中所有按钮的逻辑有什么问题 按钮状态逻辑: var friendshipStatusQuery = PFQuery(className: "Follow") friendshipStatusQuery.whereKey("status
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()
}
}
发生的情况是,查询要求下表中的任何对象的状态属性
==“Pending”
。如果该表中的一个或多个对象满足该条件,则“挂起”标题将应用于所有单元格的按钮
那么,代码中有两件事需要修正。首先,将Follow查询细化为关于当前行的查询,这可能与Follow对象中等于userArray[row]
的指针属性有关
其次,如果用户在查询运行时滚动表格,直接在查询的完成块中引用
单元格
,将产生不必要的影响。该单元格最终将被重用以表示不同的行。我的做法是将查询结果缓存到某个地方(如用户数组中),然后在indexPath处重新加载行。Hey@danh感谢您的回答。当您提到在查询中将follow对象指向等于userArray[row]时,您能提供更多的语法吗?我目前的想法是将findobjectsinbackgroundithblock
中的query if语句更改为if error==nil&&cell.addUserButton.tag==indexPath.row
,但我不确定这是否是您的意思,这取决于数据模型和表的意图,但是假设该表包含当前用户可能跟随的用户,并且由followBy表中的“followDBY”属性表示。然后查询必须询问whereKey:“followerdby”isEqualTo:currentUser。