Tableview to Tableview - ios

I am a complete novice to coding and app building so apologies beforehand if this has a really easy solution. I have created an app that has a tab bar at the bottom and then tableview on one of the tabs. I have populated the cells in this tableview.
I am looking to be able to select a cell and it then go to another tableview with different categories based on the cell selection. E.g. First table view: Dogs, Snakes, Rabbits. Then the second table view: Labrador, Shitzu, poodle (if the 'Dogs' cell is selected) Python, Cobra, Rattlesnake (if the 'Snakes' cell is selected). Etc.

You'll want to embed these two TableViewControllers in a Navigation controller.
You can do this in storyboard:
Select the TableViewController with the first table in it
In Xcode's menu bar: Editor > Embed In > Navigation controller
Create another TableViewController in the storyboard.
Ctrl+Drag a storyboard segue from a TableViewCell on the first TableViewController to the second TableViewController. Like #hw731 said, this will indicate that when a cell is tapped it will invoke the segue to the new TableViewController.
--Now move to code.
In the .m file for that first view controller you can pass whatever data you want to pass to the second VC. (like, the fact that they chose dog)
In the -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender method:
Grab the reference to the second view controller like so: SecondTableViewController *secondTableVC = segue.destinationViewController
Now you can say secondTableVC.arrayOfItemsToDisplay = dogsArray;
There are some interim steps in there like creating the class for the second TableViewController and connecting it. Hope this helps!

Related

Reusing the same viewController on top of the navigationStack in swift

For my app project i have created a tableView that show a wine including, rating reviews, tasting notes and all sorts of data in each section. In the last section the user is shown two similar wines, once pushed it is the purpose to reUse the current viewController, and throw it onto the navigation stack as the user might browse another wine inside that viewController and so forth.
How would you go about, reUsing the same viewController. Since building infinite numbers of the same viewController in storyboards seems abit rookie to me. Thanks all help appreciated.
You can use Storyboards and reuse the ViewController with no problem.
In the Storyboard use the Storyboard ID in the identity inspector, Add a unique ID to the ViewController.
After that it's pretty simple, you can ether:
Instantiate the first level viewController with the segue, than once you want to add another ViewController (of the same type) with the UIStoryboard instantiateviewcontrollerwithidentifier: and give the unique id of the ViewController, and present it
or
You can just use the UIStoryboard instantiateviewcontrollerwithidentifier: every time you want to present the ViewController
If you want to use storyboard, there is a hack (a bit dirty).
You should add button to view controller. But not to the view of
view controller, you should add it to the top section (top bar) of
your view controller.
Then you add segue from this button to this very view controller.
Now you may use this segue from code (performSegue...)

UINavigationController for infinite navigation (nested folders)

I need to navigate inside folders and files in directory (from server). The problem is that I don't know the number of folders so it's not possible to use performSegueWithIdentifier statically. How can I use navigation controller with dynamically number of view controllers in swift? I want to "push" a new view controller every time a user tap on a folder in order to list files/folders inside it and I want to do it with UINavigationController so the user have the possibility to go back with "previous" button.
Both storyboard and programmatically approaches are ok.
Thanks you
Storyboards and segues are just a crutch. Think about how you would do this without them. At each level, to go down a level, you would just instantiate a new view controller and push it onto the navigation controller stack with pushViewController:animated:.
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UINavigationController_Class/#//apple_ref/occ/instm/UINavigationController/pushViewController:animated:
And in fact it takes only one view controller class to do this, since any instance can create and push another instance of its own class. The display of one folder is exactly like the display of any other.
So if you wanted to configure this notion in a storyboard, you would have a circular segue, that is, the view controller would have a push / show segue leading to itself.
I agree with #matt's answer, just create the controller and push it. For sake of completeness, you can do this in a Storyboard with a segue.
Here's how:
So that you can call the segue programmatically, add an additional prototype cell to your tableView. (You do this because you don't want the segue to be automatically triggered when the tableViewCell is selected. By using an additional prototype cell, the segue can be wired up, but it will never be triggered automatically since this prototype cell will never actually be instantiated.)
Control-drag from this prototype cell to the viewController icon at the top of the tableViewController. Select "Show" from the pop-up.
Find this segue in the Document Outline View and give it an identifier such as "showFolderSegue" in the Attributes Inspector.
Now, when you want to trigger the segue, call: self.performSegueWithIdentifier("showFolderSegue", sender: self)
You can use prepareForSegue to set up the new tableViewController as you normally would.
This method too works with a single tableViewController.

Going into a second table view when i click on a table view cell of a first table view?

Hi i'm working on a standard Master-Detail Applicaton.
When you create a master-detail application in xcode (i mean the one that it propose when creating a new project), xcode creates a navigation controller, a table view and a detail view, the default behavior when you click the uitableview cell is to go on the detail view.
I would like instead to go on another table view (that of course i had already created in the storyboard). My purpose is to show a list of filtered values ​​on the basis of what I clicked (the cell clicked) on the first table view.
So i try to accomplish this from the graphical storyboard, i try to ctrl-drag an arrow from the first-standard table view to the second-new one. Selecting manual segue = show.
And I even deleted the "original" arrow that link the first-standard table view to the detail view (the one proposed by xcode).
But when i click on a table view cell in the simulator, the clicked cell becomes gray and nothing happens.
How can I make a second filtered table view appear when I click on a first' table view cell?
What should i do to accomplish this?
Ps. i am a very beginner of iOS programming, objective-c and cocoa touch framework. I xcode tag because i would like to know what can be done from within the storyboard (graphically) to accomplish what i need to do.
If you want to show a "detail" table view when you click onto a "master" table view cell you can do this with ctrl-drag. Start from the prototype cell (selecting it) of the master view to the second table view, in the storyboard, then choose show detail option to create a segue.
click on the segue and check its name in the attributes inspector, it should be showDetail , as in the MasterViewController.swift file you should look at this function, in prepareForSegue you can set the filtered results:
Also: setDetailItem:object should be changed to another function, since the tableView scene you have created will not have the detailItem property , you can add a property in the h file and set it from prepareForSegue
#pragma mark - Segues
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSDate *object = self.objects[indexPath.row];
[[segue destinationViewController] setDetailItem:object];
}
}
This is very basic. In short: before you switch to the new TableViewController you fill it with filtered data in prepareForSegue according to which cell was clicked.
Apple doc to this subject: Apple Doc - Navigating a Data Hierarchy with Table Views
A good answer is here: https://stackoverflow.com/a/19806545/1195661

how to implement multiple segues using dynamic cells in Xcode?

i have a table view with dynamic prototype cells divided into 2 different sections named "Forums" and "Threads". When i click on a tableview cell from the Forums section, i want to transit to the same page with a different data to display while if i click a cell from the Threads section, it should open a different scene. In short, two different types of transition segues from 2 different tableview sections)
Can anyone please help me with this?
Make sure the table view controller is embedded in a navigation controller.
Hook up two push segues to the table view controller (not to the cells).
Give those segues appropriate identifiers in the storyboard using the attributes inspector tab on each segue.
In -tableView didSelectRowAtIndexPath: add an if statement to detect which section the row tapped was in.
In the branches of the if statement, call -performSegueWithIdentifier on your controller using the identifier of the appropriate segue .
If you need to set up anything in the view controller you're seguing to from the table view, override prepareForSegue: sender: in the table view controller.
Very helpful, it worked for me, but I had to used BOTH didSelectRowAtIndexPath AND prepareForSegue, while I was reading in other posts that this is not very good.
I read that we need to use only the prepareForSegue if we need to set up any stuff...
Do you think that I should continue with both of them?
Thanks!

using viewForHeaderInSection method in iOS application

I have a grouped UITableView with multiple segments. I would like to have a button below each segment in the footer that when pushed would take the user to a different view controller (with some options - the data-model to be loaded in the destination VC). The two ViewControllers in question are managed by a UINavigationController.
I am not sure what the best way to achieve this is...
I have tried:
Creating each UIView (with the button) in IB and trying to add the view to a section footer there. I do not think this is possible.
Creating the UIViews purely in code, add then adding them to each of the section footers. This is working fine however the issue here is that I then cannot wire in my segue & segue identifier in IB. I am using the prepareForSegue method to send the correct data-model to the destination VC (depending on which segment footer button is pushed) so I require this. Is there an alternative here, can I trigger send my data-model in some other way?
Thanks, James
1 is possible.
Pull your customer view out of the nib as you would any other custom view Load View from Nib; then add it using:
- (UITableViewHeaderFooterView *)headerViewForSection:(NSInteger)section
edit Sorry, didn't mean to imply that #2 isn't also viable.
- (id)instantiateViewControllerWithIdentifier:(NSString *)identifier
// like this:
UIViewController * vc = [[self storyboard] instantiateViewControllerWithIdentifier:#"TheNewView"]
[self.navigationController pushViewController:vc animated:YES];
Using option 2, Create your segue from the view controller instead of from a specific view or button. Give it a meaningful identifier, then in the action method for your button, call performSegueWithIdentifier:sender: on your view controller. You can pass in any object you like as the view controller, including the index path or an NSNumber holding the section identifier, so you can access this in prepareForSegue and use the appropriate parts of your data model.

Resources