我正在使用tableview单元格中的按钮来创建当前登录用户与已单击的tableview单元格中显示的用户之间的关系。我已经能够为此tableview单元格按钮创建操作功能,并且可以确认由于我的日志而注册了单击,但是我的Parse保存块的else语句正在使用值nil
触发。我是否试图用我的解析代码错误地建立这种关系?
TableView代码:
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
cell.addUserButton.tag = indexPath.row
cell.addUserButton.addTarget(self, action: "addUser:", forControlEvents: .TouchUpInside)
let users: PFObject = self.userArray.objectAtIndex(indexPath.row) as! PFObject
if let user : PFUser = users as? PFUser {
cell.usernameLabel.text = user.username
println("user exist")
if let userImage : PFFile = user["profileImage"] as? PFFile {
userImage.getDataInBackgroundWithBlock{
(imageData: NSData!, error: NSError!) -> Void in
if error != nil {
let image: UIImage = UIImage(data: imageData)!
cell.userImage.image = image as UIImage
}
}
} else {
cell.userImage.image = UIImage(named: "profileImagePlaceHolder")
}
}
return cell
}
func loadParseData() {
var query : PFQuery = PFUser.query()
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: AnyObject) {
println("Button Triggered")
let addUserButton : UIButton = sender as! UIButton
let user : PFObject = self.userArray.objectAtIndex(addUserButton.tag) as! PFObject
var relation : PFRelation = PFUser.currentUser().relationForKey("Followers")
relation.addObject(user)
PFUser.currentUser().saveInBackgroundWithBlock {
(succeed:Bool, error: NSError!) -> Void in
if error != nil {
println("Added User")
} else {
println(error)
}
}
}
}
TableView Cell:
import UIKit
class SearchUsersRegistrationTableViewCell: UITableViewCell {
@IBOutlet weak var userImage: UIImageView!
@IBOutlet weak var usernameLabel: UILabel!
@IBOutlet weak var addUserButton: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
userImage.layer.borderWidth = 1
userImage.layer.masksToBounds = false
userImage.layer.borderColor = UIColor.whiteColor().CGColor
userImage.layer.cornerRadius = userImage.frame.width/2
userImage.clipsToBounds = true
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
更新:数据实际上是正确发送到解析,但我不确定为什么我的else语句被解雇。
答案 0 :(得分:0)
if
中PFUser.currentUser().saveInBackgroundWithBlock
块内的addUser(...)
语句看起来不对。您正在测试错误是否为!=
(即出现错误)。也许如果它测试nil如下...
if error == nil {
println("Added User")
} else {
println(error)
}
...你会看到你期望的日志记录。