I created a menu inside of my app.
The menu has 4 cells, which are created by modifying a prototype cell.
Each of those cells should perform a segue to one of 4 ViewControllers.
How can I realize this? Any ideas?
You can either:
1) Create 4 prototype cells having each one wired directly to its respective viewController. In this case, each give each prototype cell its own identifier (for example "cell1", "cell2", "cell3" and "cell4") and then in cellForRowAtIndexPath dequeue using the appropriate identifier for the row: ["cell1", "cell2", "cell3", "cell4"][indexPath.row].
OR
2) Wire the segues from the viewController icon at the top of your viewController, give the segues identifiers, and trigger the appropriate segue with performSegueWithIdentifier in didSelectRowAtIndexPath.
Related
I would like to have an UIPageViewController with several pages where all of them are instances of UITableView or even UITableViewController, but I am a bit lost on how to do this. I have tried adding them from the Interface Builder, but this doesn't seem to be possible.
I'm assuming you have your PageVC and you have connected VCs. Suggest use standard VCs in preference to TableVCs:
This is how I setup a TableView in a View Controller
In Storyboard:
Drag a TableView into the View area. Select the VC icon (leftmost) in the VC header in the storyboard and in Attributes inspector ViewController layout uncheck Adjust scroll view inserts (this gets rid of some extra space at the top of the tableview.
Click the tableview to select it and cntl-drag from the tableview to again the VC icon and it will offer connect to the TableViewDelegate and to the TableViewDatasource. Select one and repeat for the other.
Then drag a TableViewCell into the TableView. Select it and, in the Attributes Inspector, set the identifier to Cell (or whatever but same as in CellForRowAtIndexPath - see below).
Next, in the XCode's top bar click the assistant editor button to get 2 windows. Have storyboard in left and the View controller in the other.
For simplicity, use a separate Swift file for each VC.
So create an outlet by dragging from the TableView in Storyboard to the VC code call it tableView (at least for now). We're done with Storyboard so can go back to the Standard Editor and focus on the VC's code.
Next, add the 2 tableview protocols UITableViewDelegate and UITableViewDataSource to the VC's source class statement.
Do an Xcode Cmd-S (Save); Shift-Cmd-K (Clean); Cmd-B (build) at this point to give the autocomplete process a nudge.
Lastly, create the 3 main TableView methods - and they should autocomplete:
NumberOfSectionsInTableView.....
return 1 // for now
NumberOfRowsInSection.....
return array.count // Assuming your data is in an array called array
CellForRowAtIndexPath.....
then
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.textLabel?.text = array[indexPath.row] //- assuming string array
return cell
That should setup your functioning tableview - Repeat for other VC(s).
Tailor accordingly...
I have the following situation:
There's a table view controller that displays all font family names, and it's inside a storyboard
Inside the UITableViewController in the storyboard there's a cell with identifier "FamilyName" that has also a disclosure indicator linked to a segue
I added a UISearchController that should filter the results of the previous table view controller
Inside the table view controller that acts as results updater I registered the same reuse identifier:
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "FamilyName")
And I'm calling dequeueReusableCellWithIdentifier:forIndexPath:, but I get a different UITableViewCell. I'd like to get the same cell that is in the storyboard inside the other table view controller, so that it's already linked to a segue.
Reuse your tableView. Don't make a new one just change the data source. This should work as intended. Just modify the data source and update the table.
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "FamilyName")
That means: Do not use the storyboard to get the cell. So you can hardly be surprised when the cell doesn't come from the storyboard: you said not to. The only way to get a cell from a storyboard is for the table view to register no class or nib for this cell reuse identifier.
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
Can someone tell me definitively if there is a limitation with using Unwind Segues with UITableViewCell or if not how to achieve this?
I can successfully implement an unwind segue from my second view controller to my first - triggered using a simple UIButton.
However, when I try to use a UITableViewCell to create the unwind segue (drag from the TableViewCell to the green Exit icon then I do not get an option for Action Segue. Instead I get options for "Selection Segue" and "Accessory Action".
How can I trigger the unwind segue on selection of a UITableViewCell?
Selection segue will fire when you select the cell. Accessory action will fire when you tap the cell accessory. unwind: underneath the Selection Segue is the one you want to choose here:
Ok. I'm a little embarrassed at my error here. I hadn't created an outlet for my UITableView and was instead using a tableview that I had created and added through code in my ViewController. Therefore the "Scene Exit" that I hooked up to the UITableViewCell on my storyboard was never being called (because the UITabelViewCell that it was hooked up to was not the one that was visible when the app ran)
I simply created an outlet to the table view and used that and all was fine. I'll put it down to my newbie status :-/
I have an undefined number of levels (depth) in my tableview, so i'd like to be able to create a segue between the view and itself so that i can call it as long as i have sub levels to discover. How can i do that ?
I've thought of directly using [self.navigationView pushViewController] when i need it, but i'd like the new controller to use the defined view in my SB.
Thanks,
EDIT: For now the only solution i've found is duplicating the VC, setting a segue between VC1 and VC2 called 'showNext', and a segue between VC1 and VC2 called the same way. Isn't there a better solution ?
If you're using dynamic cell prototypes, you obviously can do a segue from the table view cell to the controller without any problem.
When you make your segue, you end up with:
But let's imagine for a second that there's some reason that doesn't work for you, e.g. you could not have a segue from the cell (for example, you need to invoke segue from didSelectRowAtIndexPath, not upon selecting the cell). In this case, you couldn't use that previous technique.
There are a couple of options in this case:
As pointed out by Chris, if only supporting iOS 6 and above, use the above technique, but (a) make sure your didSelectRowAtIndexPath uses self for the sender and then have shouldPerformSegueWithIdentifier only allow the segue if the sender == self (thus when the sender is the table view cell, it will be canceled);
Perhaps even easier, just don't define a segue at all and have your didSelectRowAtIndexPath manually instantiateViewControllerWithIdentifier and then push/present that view controller as appropriate; or
You can use the following, kludgy work-around: In short, add a button to your view controller (drag it down to the bar at the bottom), where it won't show up on the view, but you can use its segues. So first, drag the rounded button to the controller's bar at the bottom of the scene:
Now you can make a segue from that button to your view controller:
And thus, you end up with the self-referential segue again:
You must give that segue an identifier, so you can invoke it programmatically via performSegueWithIdentifier, but it works. Not the most elegant of solutions, but I think it's better than having an extra scene on your storyboard.
None of these are ideal, but you have lots of options.
This process worked for me to do the exact same thing...
Set up your tableview in storyboard as usual. DO NOT give your prototype cell a Cell Identifier. Set up your segue as you did above, by dragging from the cell to the controller.
In your viewDidLoad register the cell class
//register the cell identifier as we are not loading form a storybard
[_aTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"CellID"];
In your cellForRowAtIndexPath set up your cell
static NSString *CellIdentifier = #"CellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
Now you can use didSelectRowAtIndexPath and call performSegueWithIdentifier. the prepareForSegue will only be called once.