ios - 使用 Button 从 UICollectionView 查找和删除视频 - Swift

标签 ios swift video uicollectionview uicollectionviewcell

问题:我找不到按下的删除按钮并将其与单元格中的视频关联,以便我可以在 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)
}

关于ios - 使用 Button 从 UICollectionView 查找和删除视频 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32660463/

相关文章:

ios - 将 UIView 插入背景并在顶部显示文本字段/按钮(SWIFT)

iphone - 使用 cornerradius IOS 使其变圆时发出相关的 UIimageView

Swift 3 通过 `Error` 发送自定义 `Notification` 不能在观察方法中转换为 `Error`

ios - 如何在 MVVM 模型中使用 MKMapViewDelegate 方法

ios - 更改 map 注释 View 的图像原点?

ios - iOS 中的自定义 View 类未调用委托(delegate)事件

jquery - 没有控件的 iPad html5 视频?

javascript - 检测 youtube 视频何时结束 - javascript - 不起作用

html - 你能为海报属性提供后备吗?

ios - 如何在解析中检索单列数据?