Adding two Table Views to the main View in Xcode
If you want 2 tableviews in the same view controller do the following:
Create a UIViewController that is a delegate to
UITableViewDelegate, UITableViewDataSource
- Add 2 uitableviews to this view controller in the storyboard
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;
Connect the IBOutlets to these.
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; } }
Burning Hippo
Updated on June 04, 2022Comments
-
Burning Hippo almost 2 years
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.
-
Burning Hippo almost 10 yearsI 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!
-
hackerinheels almost 10 yearsI 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.
-
May Phyu about 7 years@hackerinheels, bro Could you help me my problem stackoverflow.com/questions/43757311/… ?