2015-11-02 139 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

相關問題