我如何使用视图控制器制作 table 视图 swift
How I can make a table view with view controllers In swift
我可以制作一个具有 table 视图的应用程序,但我需要单元格将我重定向到 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
}
}
尝试为 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
我觉得对你有帮助。
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())
}
}
我想你会想要制作另一个带有网络视图的视图控制器。然后,每当您 select table 中的一个单元格时,转到它并传递一个字符串,例如视频的 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
我可以制作一个具有 table 视图的应用程序,但我需要单元格将我重定向到 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
}
}
尝试为 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
我觉得对你有帮助。
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())
}
}
我想你会想要制作另一个带有网络视图的视图控制器。然后,每当您 select table 中的一个单元格时,转到它并传递一个字符串,例如视频的 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