Swift:uitableViewCell中的IBaction

时间:2014-09-07 09:37:21

标签: uitableview swift parse-platform ibaction

我在单元格中有一个按钮。 当我按它时我有这个错误:“在解开一个可选值时意外地发现了nil”

有什么想法吗? 这是我的代码:

    @IBAction func followButton(sender: AnyObject) {


if tableView != nil {

    if var indexPath :NSIndexPath = self.tableView.indexPathForSelectedRow() {



        let user:PFObject = self.userList.objectAtIndex(indexPath.row) as PFObject
        var relation : PFRelation = PFUser.currentUser().relationForKey("KfriendsRelation")
        relation.addObject(user)
        PFUser.currentUser().saveInBackgroundWithBlock { (succeed:Bool, error: NSError!) -> Void in
            if error != nil {

            }

        }


    }


   println("not working")

}

1 个答案:

答案 0 :(得分:0)

此时您的tableView可能是nil,因此请仔细检查。

此外,当用户点击单元格中的按钮时,该按钮会处理触摸,因此可能无法选择此单元格。您可以在tableView上使用UITapGestureRecognizer来处理点击,或者使用followButton(sender:)方法获取触摸位置,然后使用tableView.indexPathForRowAtPoint(_:)方法获取索引路径。

相关问题