Issues using custom iOS UITableView cells / "Multiple segues with identifier" error - uitableview

I have created a Table View that has multiple Table View Cells of different styles - say "CellStyle1" and "CellStyle2".
The cells look different, but they should perform the same when selected - namely, segueing to the same new view.
I have set up both cells to have the same Storyboard Segue - "PushView", pointing to the same view controller.
This seems to work OK, but Xcode generates a warning: "Multiple segues with identifier"
How can I avoid this error? What is the right way to handle multiple custom cells that look differently but act the same and should segue to the same place?
This one has me scratching my head.
Thanks!

You could make one segue by ctrl dragging from the view controller to the destination view controller. And then assuming tapping the cell is the event that you want to trigger the segue, in didSelectRowAtIndexPath call performSegueWithIdentifier.
- (void)performSegueWithIdentifier:(NSString *)identifier sender:(id)sender
Or just give the 2 segues different identifiers and in prepereForSegue check for either identifier.

Related

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.

Segue between multiple detail views when selecting from tableview

Ok, I haven't been able to find a definitive answer to this. I'm on Xcode 6 working with Swift and a UISplitView. I have multiple detail views in storyboard and I want to be able to replace my detail view with another when selecting from a tableview. In Xcode 5, I was able to drag multiple segues from my prototype cell and use performSegueWithIdentifier. In Xcode 6, I can only have one segue coming from my prototype cell.
What's the best way to go about this?
One type of cell, one segue. This seems to be a logical and useful system constraint.
One solution is to just create more cell types with different cell identifiers.
Alternatively, if you have another way to determine which segue to use, draw all segues from the view controller instead of the cell. You can attach the necessary row object information in the sender object when calling the segue from the cell selection method.
You can create as many segues as u want from viewcontroller and give each seuge an different segue Identifier in IB, and the call each with performSegueWithIdentifer method

Static UITableView in Storyboard, with programmatic segue

I have a storyboard that has a UITableViewController. The table view has static cells, and I've configured the cells to transition to other view controllers when tapped (by control-dragging them to set up segues). This works great.
Now I've added a row to the table, and for this row I need to transition to a UIViewController that's implemented in a different storyboard. I need to handle this one in code.
Is there a way to handle the tableView:didSelectRowAtIndexPath: for this one row in code, while letting the storyboard handle the rest of the rows which already have transitions defined?
I tried calling super within the tableView:didSelectRowAtIndexPath: to invoke the "default" behaviour, but that doesn't work (undefined selector on UIViewController). I want to invoke the storyboard's behaviour for all the rows except the one I want to handle programmatically.
Sure, you can do that. Implement didSelectRowAtIndexPath, and put in an if statement that executes the code you need to go to the other storyboard controller only for the row that you just added (and do nothing for any other row).

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