【问题标题】:How I can make a table view with view controllers In swift我如何在 swift 中使用视图控制器制作表格视图
【发布时间】:2015-06-19 02:36:56
【问题描述】:

我可以制作一个带有表格视图的应用程序,但我需要单元格将我重定向到 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

}

}

【问题讨论】:

    标签: ios xcode swift tableview


    【解决方案1】:

    尝试实现选择单元格的委托方法

    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

    【讨论】:

      【解决方案2】:

      我认为它对你有帮助。

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

      【讨论】:

        【解决方案3】:

        我认为您想制作另一个带有 Web 视图的视图控制器。然后,每当您在表格中选择一个单元格时,转到该单元格并传递一个字符串,例如视频的 URL。

        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

        【讨论】:

        • 另外,将故事板上的 segue 从一个视图控制器连接到下一个视图控制器。如果您从 TableView 连接它,它会在您设置要传递的数据之前进行 segue。这就是为什么您需要使用“performSegueWithIdentifier”以编程方式调用 segue。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-04-04
        • 2014-06-11
        • 2017-10-02
        • 2017-06-10
        • 2023-04-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多