如何使用视图控制器创建表视图在swift中

时间:2015-04-13 17:14:38

标签: ios xcode swift tableview

我可以创建一个带有表视图的应用,但我需要单元格才能将我重定向到YouTube视频。当我按下一个单元格时,我想制作一个youtube视频,或者将我发送给另一个视图控制器。请帮帮我

 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

}

}

3 个答案:

答案 0 :(得分:0)

尝试为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

答案 1 :(得分:0)

我觉得它对你很有帮助。

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())
    }
}

答案 2 :(得分:0)

我认为您想要制作另一个带有Web视图的视图控制器。然后,只要您在表格中选择一个单元格,就转到它并传递一个类似于视频网址的字符串。

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]
  }
}

另见: Pass data through segue