Search code examples
uitableviewswiftparse-platformibaction

Swift/parse : IBAction in a tableViewCell to make pfrelation


I would like to implement an IBAction in a tableViewCell. The tableView dysplay a list of users, and for each user, i have a follow button.

The follow method needs to specify the user to follow witch is the cell's user.

How could i do this ?

 override  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell: AddFirendsTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)as AddFirendsTableViewCell

    let users:PFObject = self.userList.objectAtIndex(indexPath.row) as PFObject
   return cell
   }

  override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath:   NSIndexPath) {

    let users:PFObject = self.userList.objectAtIndex(indexPath.row) as PFObject




}

@IBAction func followUnfollow(sender: AnyObject) {

    //what should i put here ?


}



func follow (user :PFUser) {


    var relation : PFRelation = PFUser.currentUser().relationForKey("KfriendsRelation")
    relation.addObject(user)
    PFUser.currentUser().saveInBackgroundWithBlock { (succeed:Bool, error: NSError!) -> Void in
        if error != nil {
            println(error)
        }
    }

}

Solution

  • I'm a little confused on how you are going about it, though I will just run with it for a moment. In order to get a reference to a cell, you could use the following line:

    let indexPath = tableView.indexPathForRowAtPoint(buttonPosition)
    

    You could then use the index path to get a reference to the cell, and therefore do what you like with it:

    let cell = tableView.cellForRowAtIndexPath(indexPath)
    

    However, i would personally move the IBAction into the cells subclass of AddFirendsTableViewCell where you already have access to all of the cells variables and values. This then means that every cell will work the same and you won't have to do some weird work-around to try and find out which button got pressed. But, that is just my opinion. Hopefully this will have helped.