2015-11-02 85 views
-1

我有一个按钮的自定义单元格,我正在尝试向它添加一个操作。出于某种原因,该应用程序崩溃的错误unrecognized selector sent to instance。我该如何解决?我的语法似乎正确。为什么点击按钮时程序崩溃?

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

    let cell = collectionview.dequeueReusableCellWithReuseIdentifier("searchcell", forIndexPath: indexPath) as! searchcustomcell 

//follow is the name of the button in the custom cell 
     cell.follow.addTarget(self, action: "FollowUser:", forControlEvents: UIControlEvents.TouchUpInside) 


    func FollowUser(sender: UIButton){ 
     var relation: PFRelation = (PFUser.currentUser()?.relationForKey("Friendship"))! 
     relation.addObject(users[indexPath.row]) 

return cell 
    } 
+2

哪个选择器和哪一类实例? (最好将整个错误信息粘贴到问题中,而不是试图描述它。) –

+0

http://puu.sh/l7awG/7eeebf1813.png – xxtmanxx

回答

1

出于某种原因与错误无法识别选择的应用程序崩溃发送到实例。

不是“由于某种原因”。那的原因。您正在向一个实例发送无法识别的选择器。问题是你的FollowUser函数不是一种方法;它隐藏在您的collectionView:cellForItem...方法中。剪下它,并将其粘贴到您课堂的顶层:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    // ... 
} 
func FollowUser(sender: UIButton){ 
    // ... 
} 
+0

问题是我需要访问FollowUser函数中的'cellForItemAtIndexPath' 。你的方法不允许。无论如何避免这个错误,并有FollowUser函数可以访问'cellForItemAtIndexPath'? – xxtmanxx

+0

我回答了你问的问题。如果你按照我所说的去做,你就不会崩溃。问题解决了。如果这会引发一个新问题,请提出一个新问题,但是您必须做出我说要做的改变,否则您就会崩溃。 – matt

相关问题