I placed two Table View's onto my main.storyboard. I then created two new .swift class files for them: VideoTableViewController and LinkTableViewController. How do I link the two? It looks like I may have done this wrong because I read I need to have the delegate linked to the View that the Table View is on. I want it linked to the controller I created for the Table View. What I am trying to do is have two lists side by side, one with video links and another with web links. I already have the data in arrays. I just need to get that data into the cells of the proper table view.
I noticed I can drop a Table View Controller onto the storyboard, but i don't think that 's what I want since it's an entirely different view. I would like for these to be on the same view.
VideoTableViewController:
import Foundation
import UIKit
class VideoTableViewController: UITableViewController {
@IBOutlet weak var videoTable = UITableView()
var videos: [Video] = []
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
//disable header
tableView.contentInset = UIEdgeInsetsMake(-30, 0, -20, 0);
var cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Component") as UITableViewCell
if (cell == nil) {
cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "Component")
}
//cell.textLabel.text = "Baking Soda"
//cell.detailTextLabel.text = "1/2 cup"
return cell
}
override func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
return 1
}
override func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
}
override func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
return self.videos.count
}
func setVideos(videos:[Video]) {
self.videos = videos
}
}
The main ViewController:
This fills the arrays of objects then passes the array to the proper table controller.
import UIKit
class ViewController: UIViewController {
//arrays to hold objects
var videos :[Video] = []
var links :[Link] = []
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.fillArrays()
//instantiate TableViewController
let videoTable = VideoTableViewController()
let linkTable = LinkTableViewController()
videoTable.setVideos(videos)
linkTable.setLinks(links)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func fillArrays() {
let file = File()
let path = "/Users/me/file.rtf"
if (file.exists(path)) { //if file exists
let read :String? = file.read(path) //read the file
let content :String = read! //get file content
let lines = content.componentsSeparatedByString("\n") //explode content on line breaks
var lineItem :[String] //to hold each item of line explode
for line in lines { //loop through lines
lineItem = line.componentsSeparatedByString(",") //explode line on commas
//instantiate an object for the line, initialize it, and append the object to its array
switch lineItem[0] {
case "L":
self.links += Link(image: lineItem[0], path: lineItem[1])
case "V":
var duration :Int = 100
self.videos += Video(image: lineItem[1], path: lineItem[2], title: lineItem[3], duration: duration)
default:
break
}
}
}
}
}
So, am I going about this wrong? Apparently it is an odd thing I am doing since I can't find much straight forward info on it Googling.