2014-09-06 35 views
-1

我想在tableViewCell中实现一个IBAction。 tableView不能显示用户列表,对于每个用户,我有一个关注按钮。Swift /解析:IBAction在tableViewCell中做出pfrelation

以下方法需要指定用户跟随女巫是单元格的用户。

我怎么能这样做?

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) 
     } 
    } 

} 

回答

3

我对你是怎么回事有点困惑,尽管我会一直运行它一会儿。为了得到一个单元格的引用,你可以使用下面一行:

let indexPath = tableView.indexPathForRowAtPoint(buttonPosition) 

然后,您可以使用索引路径才能到单元格的引用,因此,做你用它想:

let cell = tableView.cellForRowAtIndexPath(indexPath) 

但是,我亲自将IBAction移动到AddFirendsTableViewCell的单元格子类中,您已经可以访问所有单元格变量和值。这意味着每个单元格的工作原理都是一样的,你不需要做一些奇怪的工作来试图找出哪个按钮被按下。但这只是我的观点。希望这会有所帮助。