问题:我无法找到我按下的删除按钮,并将其与单元格中的视频绑定,以便我可以在Parse上删除它。
我在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 :(得分:0)
将目标函数更改为:
的名称。在这里,您可以将按钮传递给目标函数。
self.deleteButton.addTarget(self, action: "deleteCellFromButton:", forControlEvents: UIControlEvents.TouchUpInside)
在您的函数中使用您的button.tag
:
func deleteCellFromButton(button: UIButton) {
println(button.tag)
}