I have a custom tableview cell that displays a Parse users profile image, username label and a button to add them as a "following" object within my Followers class. My relation setup is set as the "follower" within my "Followers" class as the username of the current user and the "following" as the username of the tableview cell that the "add friend" button was clicked. The way I am currently trying to achieve this is add a tag to the button that is set to indexPath.row and then adding a target and IBAction. From there I set the label view with a tag value of 1 and then try to set that label as a usernameLabel variable which I can use to set to the "following" column. Unfortunately my current solution, following["following"] = usernameLabel.text as PFObject gives me 'UIView? is not convertible to 'PFObject'. Is this the best way to achieve what I'm looking to do and any idea why this error is happening?
Here is my tableview cell:
import UIKit
class SearchUsersRegistrationTableViewCell: UITableViewCell {
@IBOutlet var userImage: UIImageView!
@IBOutlet 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
}
}
Here is my tableview functionality. (Note that this is a view controller with a tableview element):
import UIKit
class SearchUsersRegistrationViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var userArray : NSMutableArray = []
@IBOutlet var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
loadParseData()
var user = PFUser.currentUser()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
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")
}
}
}
let textCellIdentifier = "Cell"
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.userArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, 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
profileImage.getDataInBackgroundWithBlock({
(result, error) in
cell.userImage.image = UIImage(data: result)
})
cell.usernameLabel.text = username
cell.addUserButton.tag = row
cell.addUserButton.addTarget(self, action: "addUser:", forControlEvents: .TouchUpInside)
return cell
}
@IBAction func addUser(sender: UIButton){
let usernameLabel = sender.superview?.viewWithTag(1)
var following = PFObject(className: "Followers")
following["following"] = usernameLabel.text as PFObject
following["follower"] = PFUser.currentUser().username //Currently logged in user
following.saveInBackground()
/* if let surtv = tableView.objectAtIndex(sender.tag) as? SearchUsersRegistrationTableViewCell {
surtv.usernameLabel = sender.tag as! String
var following = PFObject(className: "Followers")
following["following"] = usernameLabel.text
following["follower"] = PFUser.currentUser().username //Currently logged in user
following.saveInBackground()
}*/
}
@IBAction func finishAddingUsers(sender: AnyObject) {
self.performSegueWithIdentifier("finishAddingUsers", sender: self)
}
}