How can I integrate an UITableView or UITableViewController into a UIPageViewController? - ios

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...

Related

Create different segues from one tableView

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.

How to reuse a cell that is inside another table view controller

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.

How to create a Segue on the Storyboard, from a programmatically registered UICollectionViewCell to a new UIViewController?

I'm using Storyboard in Xcode 5.1.1 and I have a UICollectionView there. However, the cells there actually use an XIB (so, not displayed on the Storyboard), which is then programmatically registered to the UICollectionView, like so:
UINib *nibHero = [UINib nibWithNibName:#"MTNNewsListHeroCell"
bundle:[NSBundle mainBundle]];
[self.newsListCollectionView registerNib:nibHero
forCellWithReuseIdentifier:#"heroCellID"];
So far, so good. Everything is working fine.
Next, I would like to create a segue from this UICollectionViewCell to another UIViewController on the Storyboard. It's a pretty standard "click an item, open the detailed view" feature.
Normally, if the Cell exists on the Storyboard, inside the UICollectionView, I can draw a line on the Storyboard from it to the next UIViewController to create the Segue. However, the Cell does not exist on the Storyboard (again, because the cells are made as an XIB file):
The goal is simply to make it so that other programmers who read my code can understand the flow of the app better, using the Storyboard.
Is it possible to show and create a Segue on the Storyboard, from a programmatically registered UICollectionViewCell (made inside a UICollectionView that's a subview of News List View Controller, in my example), to a new UIViewController (the Single News view, in my example), and have that connection actually show up on the Storyboard?
Final update:
This is what I ended up doing, after following the suggestion from user #rdelmar. I think it does the job nicely:
No, you can't do that. A segue is basically a visual representation of a transition from one view controller to the next. You need both ends of that segue to be visually represented on the storyboard to make one. You could add a cell into your collection view and connect a segue to it just to show the flow in the storyboard, but it would never be invoked because the cell you actually have in the collection view will have been created from the xib file (I'm not sure that would make things clearer to someone looking at your code though). If you really want an actual functioning segue, then make the cell in the storyboard instead.
There can be workaround if someone later on needs this.
In the collectionview or collectionviewcontroller:
private var onSelectionChangeListener:(()->Void)? = nil;
override func collectionView(collectionView: UICollectionView, shouldSelectItemAtIndexPath indexPath: NSIndexPath) -> Bool {
if onSelectionChangeListener != nil {
onSelectionChangeListener!();
}
return true;
}
func setSelectionChangeListener(listenerMethod:(()->Void)) {
onSelectionChangeListener = listenerMethod;
}
This way your collection view can understand there has been a selection. You can move on from here. Inside your custom listenerMethod, you can call segue instead of inside the cell.
You may need to change the method signature ( rather than ()-Void ) to get which cell was interacted with.

Static table view outside UITableViewController

After the new Xcode update, my app doesn't validate and shows this error:
static table views are only valid when embedded in UITableViewController instances
Any chances to solve easily?
The only way to get a static UITableView along with other controls on the same screen is to use a Container View. Follow this way, it works perfectly:
Drag a ViewController onto your storyboard.
Drag a TableViewController onto your storyboard.
Next Drag a Container view to your ViewController and size it about the size you want (smaller than the view) -> when you drag the container view it will create a segue and another view. Remove that segue and view.
Then finally ctrl click in your container and drag to your new TableViewContoller. Select Embed.
Style your TableView the way you want -> including static cells.
This answer was already found and given here: G.Huebner -> http://web.archive.org/web/20140928102504/http://iphonedevsdk.com/forum/iphone-sdk-development/111800-static-table-view-cells-only-work-in-a-uitableviewcontroller.html
A static UITableView must be in a UITableViewController. So you will have place the table in UITableViewController and then add it as childView to the MainViewController.
You can refer
https://iphoneidoit.blogspot.in/2013/10/static-uitableview-in-uiviewcontroller.html
I think what you are doing is you are first dragging the ViewController on the storyboard and then dragging the tableView on the ViewController. In this way you can't use the Static table cells. Instead of this what you should have done is Drag the TableViewController on the main storyboard instead of a ViewController and then select the static cells. In this way you can be able to work on static cells and can use any elements on static cells.
Well, if you are really using a static tableView, you should consider copy/past everything to a ScrollView or StackView.

Tableview to Tableview

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!

Resources