使用按钮从 UICollectionView 中查找和删除视频 - Swift
Finding and deleting a video from UICollectionView with Button - Swift
问题:我找不到我按下的删除按钮并将其与单元格中的视频相关联,以便我可以在 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)
}
将您的目标函数更改为名称 :
。在这里,您将按钮传递给目标函数。
self.deleteButton.addTarget(self, action: "deleteCellFromButton:", forControlEvents: UIControlEvents.TouchUpInside)
并在您的函数中使用您的 button.tag
:
func deleteCellFromButton(button: UIButton) {
println(button.tag)
}
问题:我找不到我按下的删除按钮并将其与单元格中的视频相关联,以便我可以在 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)
}
将您的目标函数更改为名称 :
。在这里,您将按钮传递给目标函数。
self.deleteButton.addTarget(self, action: "deleteCellFromButton:", forControlEvents: UIControlEvents.TouchUpInside)
并在您的函数中使用您的 button.tag
:
func deleteCellFromButton(button: UIButton) {
println(button.tag)
}