0

I can make a app with a table view, but i need cells to redirect me to a youtube video. I want to make when i press one cell this show me a youtube video or send me to another view controller. Please help me

 class ViewController: UITableViewController {


var tricks = [Trick]()



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


    self.tricks = [Trick(name: "Ollie" ), Trick(name: "Kickflip"), Trick(name: "Heelflip"), Trick(name: "Varial Kickflip"), Trick(name: "Varial Heelflip"), Trick(name: "TreeFlip")]




}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) ->Int {

    return self.tricks.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)as! UITableViewCell

    var trick : Trick

    trick = tricks[indexPath.row]

    cell.textLabel?.text = trick.name

    return cell

}

}

Giovanie Rodz
  • 1,741
  • 3
  • 12
  • 7

3 Answers3

0

Try implementing the delegate method for did select cell

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: true)

    let trick = tricks[indexPath.row]
    //Now you have you selected trick so push in a your video playing view controller with the url from your trick object

}

here is a thread that is about playing you tube video's within your app here

Community
  • 1
  • 1
Dan Atherton
  • 161
  • 6
0

I think it helps you.

func selectRow() -> Tricks? {
    let selectedRow = self.tableView.selectedRow;
    if selectedRow > 0 && selectedRow < self.tricks.count {
        selectedRow.description
        return self.tricks[selectedRow]
    }
    return nil
}

extension MasterViewController: NSTableViewDelegate {
    func tableViewSelectionDidChange(notification: NSNotification) {
        updateDetailInfo(selectedRow())
    }
}
cmashinho
  • 605
  • 5
  • 21
0

I think you'd want to make another view controller with a web view in it. Then whenever you select a cell in the table, segue to it and pass a string like the URL of the video.

var tricks = ["Ollie", "Kickflip", "Heelflip", "Varial Kickflip", "Varial Heelflip"]
var selectedRow = 0


//SELECT THE ROW  & Call THE SEGUE
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
  tableView.deselectRowAtIndexPath(indexPath, animated: true)

  selectedRow = indexPath.row
  self.performSegueWithIdentifier("myVideoSegue", sender: self)

}

//PASS THE DATE TO THE NEXT VIEW CONTROLLER
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
  if (segue.identifier == "myVideoSegue") {

    let nextVC = segue.destinationViewController as ViewController;
    nextVC.passedTrickName = tricks[selectedRow]
  }
}

See also: Pass data through segue

Community
  • 1
  • 1
Justin Lewis
  • 534
  • 1
  • 5
  • 22
  • Also, connect the segue on the storyboard from one view controller to the next one. If you connect it from the TableView, it will segue before you set the data to be passed. Which is why you need to programmatically call the segue using "performSegueWithIdentifier". – Justin Lewis Apr 13 '15 at 17:50