2015-09-18 40 views
0

問題:我無法找到我按下的哪個刪除按鈕,並將它與單元格中的視頻綁定,因此我可以在Parse上將其刪除。使用按鈕查找並從UICollectionView中刪除視頻 - Swift

我在cellForIndexAtIndexPath

self.deleteButton = UIButton(frame: CGRectMake(cell.frame.size.width/1.5, 10, 30, 30)) 
let deleteImage = UIImage(named: "deleteVideoButton.png") 
self.deleteButton.setBackgroundImage(deleteImage, forState: .Normal) 
self.deleteButton.tag = indexPath.row 
self.deleteButton.addTarget(self, action: "deleteCellFromButton", forControlEvents: UIControlEvents.TouchUpInside) 
cell.addSubview(self.deleteButton) 

的按鈕顯示在UICollectionView每個小區顯示器上編程方式添加的按鈕。
我有一個函數,調用deleteCellFromButton()。我知道我應該使用index.path,但我不能在這個函數中調用它。

func deleteCellFromButton() { 

    var alert = UIAlertController(title: "Are you sure?", message: "This video will be deleted forever", preferredStyle: UIAlertControllerStyle.ActionSheet) 

    let confirmAction : UIAlertAction = UIAlertAction(title: "Ok!", style: .Default) { (action) -> Void in 

     var query = PFQuery(className: "Movie") 
     query.whereKey("username", equalTo: (PFUser.currentUser()?.username)!) 

// here is where I think this goes query.whereKey("videoFile", equalTo: self.videoArray[index.path.row]) ??? 



     query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]?, error: NSError?) -> Void in 

      if error != nil { 

       print(error) 

      } else { 

       for object in objects! { 
        print(object) 

        object.deleteInBackgroundWithBlock({ (success: Bool, error: NSError?) -> Void in 

         //put after delete code stuff here. 

        }) 

       } 

      } 

     } 

    } 

    let cancelAction : UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { (error) -> Void in 

    } 

    alert.addAction(confirmAction) 
    alert.addAction(cancelAction) 

    self.presentViewController(alert, animated: true, completion: nil) 

} 

回答

0

將目標函數更改爲:。在這裏你將你的按鈕傳遞給你的目標函數。

self.deleteButton.addTarget(self, action: "deleteCellFromButton:", forControlEvents: UIControlEvents.TouchUpInside) 

而在你的函數中使用您的button.tag

func deleteCellFromButton(button: UIButton) { 
    println(button.tag) 
} 
+0

確定這樣奇怪的事情。每當我添加:最後它崩潰,所以我刪除它。我打印出self.deleteButton.tag,無論哪一個我點擊它返回1.我應該將標籤存儲在數組中嗎?這是如何運作的? –

+0

我的故障在崩潰時我沒有注意到在func上添加(按鈕:UIButton) –

+0

也得到了上述標籤的工作原理。謝謝。我現在不得不在邏輯中將該號碼連接到我認爲的視頻陣列。 –