如何在使用swift

时间:2015-08-06 19:27:15

标签: ios swift url video parse-platform

我试图找到从我的解析数据库中加载视频的最佳方法在我的快速项目中。我在数据库中有一个名为ProductVideo的列,我有视频的URL。我的项目在tableview中有一个产品列表,当其中一个单元被推送时,它会带你到一个新的视图控制器,显示有关的信息项目和那里有一个名为视频的按钮,我想在点击时加载视频。这是我解开对象和显示信息的代码。一些帮助将非常感谢!

@IBOutlet weak var videoView: UIView!
@IBOutlet weak var videoStatus: UILabel!
@IBOutlet weak var productVideo: UIButton!
@IBOutlet weak var productCategory: UILabel!
@IBOutlet weak var ProductImage: PFImageView!
@IBOutlet weak var productName: UILabel!
@IBOutlet weak var productDescription: UITextView!



override func viewDidLoad() {
    super.viewDidLoad()


    // Unwrap the current object object
    if let object = currentObject {
        productName.text = object["ProductName"] as? String
        productDescription.text = object["ProductDescription"] as! String
        productCategory.text = object["ProductCategory"] as? String
        videoStatus.text = object["VideoStatus"] as? String



        var initialThumbnail = UIImage(named: "question")
        ProductImage.image? = initialThumbnail!
        if let thumbnail = object["ProductImage"] as? PFFile {
            ProductImage.file = thumbnail
            ProductImage.loadInBackground()


            if self.videoStatus.text == "None"
            {
                self.productVideo.hidden = true
                self.videoView.hidden = true
            }


    }



    }





}

vvv这是在Tableview控件中.. ^^^^上面的代码来自productViewController

//override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell {

    var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! CustomCell!
    if cell == nil {
        cell = CustomCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
    }

    // Extract values from the PFObject to display in the table cell
    if let ProductName = object?["ProductName"] as? String {
        cell.ProductName.text = ProductName
    }


    // Display product image
    var initialThumbnail = UIImage(named: "question")
    cell.ProductImage.image? = initialThumbnail!
    if let thumbnail = object?["ProductImage"] as? PFFile {
        cell.ProductImage.file = thumbnail
        cell.ProductImage.loadInBackground()
    }



    return cell
}

将此添加到viewcontroller

@IBAction func productVideo2(sender: AnyObject) {


    let videoWebView = UIWebView(frame: CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height))

    let url = object["ProductVideo"] as! String // GET THIS FROM YOUR PARSE OBJECT

    let urlRequest = NSURLRequest(URL: NSURL(string: url)!)

    videoWebView.loadRequest(urlRequest)
    self.view.addSubview(videoWebView)


}

1 个答案:

答案 0 :(得分:0)

根据您的代码,它将按照以下方式完成:

override func viewDidLoad() {
super.viewDidLoad()


// Unwrap the current object object
if let object = currentObject {
    productName.text = object["ProductName"] as? String
    productDescription.text = object["ProductDescription"] as! String
    productCategory.text = object["ProductCategory"] as? String
    videoStatus.text = object["VideoStatus"] as? String



    var initialThumbnail = UIImage(named: "question")
    ProductImage.image? = initialThumbnail!
    if let thumbnail = object["ProductImage"] as? PFFile {
        ProductImage.file = thumbnail
        ProductImage.loadInBackground()


        if self.videoStatus.text == "None"
        {
            self.productVideo.hidden = true
            self.videoView.hidden = true
        } else {


        let videoWebView = UIWebView(frame: CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height))

    let url = object["ProductVideo"] as! String // GET THIS FROM YOUR PARSE OBJECT

    let urlRequest = NSURLRequest(URL: NSURL(string: url)!)

    videoWebView.loadRequest(urlRequest)
    self.view.addSubview(videoWebView)

    }


}



}





}

<强>更新

要使用此功能,您需要使用属性创建自定义UIButton子类,以保存您的cellForRowAtIndexPath方法中存在的视频网址

<强>故事板

请注意右侧班级检查器中的自定义UITableViewCell和自定义UIButton类分配。

enter image description here enter image description here

UIButton子类示例

import UIKit

class MyCustomButton: UIButton {

    var videoUrl = ""

}

UITableViewCell子类示例

import UIKit

class MyCustomTableViewCell: UITableViewCell {

@IBOutlet weak var cellBttn: MyCustomButton!

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

}

查看控制器示例

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

//MARK: - ivars
var myData = ["One","Two","Three","Four","Five"] // Really just where your video URL is in your code

//MARK: - Overrides
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

//MARK: - UITableViewMethods
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return myData.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! MyCustomTableViewCell

    cell.cellBttn.videoUrl = myData[indexPath.row]

    cell.cellBttn.addTarget(self, action: Selector("cellBttnTouched:"), forControlEvents: UIControlEvents.TouchUpInside)

    return cell

}

//MARK: - Actions
func cellBttnTouched(sender: MyCustomButton) {

    // Spawn your UIWebView here, you can get current video URL from the sender

    let thisCellsVideoUrl = sender.videoUrl
    println(thisCellsVideoUrl)


}

}

此示例在点按按钮时产生此输出: enter image description here