3

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.

2
  • Usually you want to opt for a MVC style application where a view controller handles giving the data for its views: developer.apple.com/library/ios/documentation/general/…. That being said if you are trying to connect the table views to a specific class you can change their class in interface builder. Commented Jul 29, 2014 at 15:53
  • are you saying you want only one view controller to manage 2 tableviews? Commented Jul 29, 2014 at 17:35

2 Answers 2

5

If you want 2 tableviews in the same view controller do the following:

  1. Create a UIViewController that is a delegate to

    UITableViewDelegate, UITableViewDataSource
    
  2. Add 2 uitableviews to this view controller in the storyboard
  3. Add 2 IBOutlets for the UITableViews in your ViewController.h file like this:

    @property (nonatomic, weak) IBOutlet UITableView *tableView1;
    @property (nonatomic, weak) IBOutlet UITableView *tableView2;
    
  4. Connect the IBOutlets to these.

  5. Add the delegate functions for the tableview in the .m file somethng like this:

     - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
     {
        // Return the number of rows in the section.
        if(tableView == self.tableView1){
            return whatever1;
        }
    
        if(tableView == self.tableView2){
            return whatever2;
        }
     }
    
Sign up to request clarification or add additional context in comments.

3 Comments

I guess I am just not following. If I create a UIViewController then am I not going to have to drag the controller from that box in the right-hand bottom corner onto the storyboard? I could've done that with the table views but I couldn't put that Table View Controller onto the default View Controller. I would be open to have some sort of tabbing thing on the left side of the screen that would pop out the table view when selected. However, I still don't know how one would do such a thing since I wouldn't want the table view using the entire screen's real estate. Regardless, thanks a lot!
I was proposing that you drag out a ViewController, and then put 2 Tableviews in it. You can access them both from the left tab. And they dont have to be full screen. Sorry i am not able to understand what the issue is.
@hackerinheels, bro Could you help me my problem stackoverflow.com/questions/43757311/… ?
0

@hackerinheels' proposed solution worked for me in a bit of a different setting: using a UITableView like a radio button group in a View Controller. It didn't work initially because I missed this step in hooking up the delegate and data source in viewDidLoad() of the View Controller:

tableView.delegate = self
tableView.dataSource = self

After this step is added, it started working nicely.

Even though this might be a no-brainer for experienced folks, but for a layman like me, spelling it out can be very helpful.

As for the solution for 2 TableViews in one View Controller, @hackerinheels' proposed 5 steps + the above change in viewDidLoad(), would work nicely!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.