Embedding a UITableView inside another view - iphone

I am trying to create a UITableView view (I guess a subview) inside another controller's view. Essentially here is the (simplified) structure I want for the Settings page I am designing:
SettingsViewController.h - contains standard property and method definitions.
SettingsViewController.m - controls the loading and saving of settings.
SettingsViewController.xib - the parent view for everything going on inside
3x UISegmentedControls - each one will control a different setting. They will take up about half of the screen - the top half.
1x UITableView - covers the rest of the screen and is scrollable. Scrolling shouldn't make the other controls disappear.
I tried adding a UITableView object to my .XIB file, then creating outlets for it inside the SettingsViewController.h file, but that didn't quite work. The table showed, but only in its default format - none of the DataSource methods worked.
I got rid of the code (temporarily), but if you need specifics, I should be able to reproduce it quickly and offer you some. Plus, I might do it right this time...
P.S. Using UITableView is not mandatory. If there is another handy way of displaying and editing a list of items (NSStrings really), I'd be fine with it.

if your tableView is empty and the dataSource methods are correctly implemented you probably just forgot to connect your tableView to the dataSource/delegate.
control-drag from the tableView to the viewController and set dataSource and delegate.

Related

reuse view from storyboard

I have a tableview with custom section headers. The view for the section header is defined in the storyboard and wired to an instance variable. Is there a way to request a new instance of the view from the storyboard?
In the past I have done this by having the section header defined in its own xib file and getting a new instance by using
[[NSBundle mainBundle] loadNibNamed:#"TimerViewSectionHeader" owner:self options:nil];
UIView *newHeaderView = self.sectionHeaderView;
I dont' think there is a way to do that. Best bet is to put the tableview custom header view in a separate nib and load it like you did in your code sample whenever you need to use it.
I tried to do the same thing and ran into the same problem.
I like to work with storyboards a lot and was impressed how fast I could create a working UI. However, as soon as you need to re-use views it makes a lot of sense to put those into a separate nib along with its UIViewController subclass.
You can then place a generic UIView in all the places where your re-used view should go and add the view using your ViewController:
[myReusableViewController loadView];
[myReusableViewController viewDidLoad]; // You have to handle view callbacks yourself.
[self.myReusableViewPlaceholder addSubview:myResusableViewController.view];
[myReusableViewController viewWillAppear:YES];
So to sum it up:
Use storyboard, it's great
Create the scaffold of your application in the storyboard, along with any static view (like About screens etc.)
Create re-used views in a custom nib + UIViewController subclass and add UIView placeholders in your storyboard.
In another answer I thought about some Pros and Cons of Storyboard
The solution I've come up with for this is as follows:
I have a tableview with multiple prototype cells that displays complex data. There is a segue to a detail view, and a transaction process view.
This first tableview has a search button that displays a new tableview with the results. It needs the same functionality as the main tableview that pushes it; including segues to the detail and transaction progress views so:
On storyboard, select and copy your main tableview. Deselect and paste. Create a push segue from your main tableview to your 2nd tableview; or from where ever you want to navigate to it from. Modify the 2nd tableview as you like. IE: If it requires some UI changes no problem.
Create a new viewcontroller class that is a subclass of the viewcontroller running the main tableview.
Override the data delegate in your subclass to serve up the subset of data you want.
Back in the storyboard, select your 2nd tableview controller and in the identity inspector select your subclass as the custom class.
For this solution to work smoothly, your app really needs to be managing data for the views. You could use prepareforsegue to pass data from 1st tableview to the second, but I've found the app data model far more flexible from numerous points of view.
Unless you have buttons that push to the sub views via segue, your subclass will need to override functions that push via segues with identities. NB Segues must have unique identifiers if you id them at all.
It took a lot of trial and error to figure this out, but once you understand the concept, it's a relatively smooth solution that is quite adaptable and not so bad to implement.
I am not sure about just views, but the way that I was able to get view controllers out of my storyboard is as follows.
UIViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:#"IdentifierName"];
From here, perhaps you might be able to use this similarly to how it was once done with nibs.
I've been able to reuse a view in the storyboard just by connecting a transition from one tableview into the one I want to reuse.
so my tableview that I want to reuse is pointed to twice.
It sort of works but the problem I'm running into it setting a variable (using instantiateViewControllerWithIdentifier) in my app delegate to my table view that is getting reused.
It seems that if I reuse it, the storyboard is creating 2 instances of my tableview and the one I get with instantiateViewControllerWithIdentifier isn't the one I want.
I'm not really sure if this is the proper way to do it. But I assume many others are doing this somehow. With the custom table cells in storyboard I suspect lots of people want to reuse their views.
For example: We want to reuse the view(include subviews) in storyboard shown below.
The best solution I know so far is clip and paste the view related code to the New Singe View file without losing the information.
Detailed steps are as follows
Step 1: Rename the view we want reuse. Just prepare for step 2.
Step 2: Open storyboard as source code in order to clip the XML code we need
Step 3、4: Search and clip the code we need
Step 4.5(Not needed): Open as Interface Builder to see the view removed
Step 5、6: New XXX.xib and paste the code we clipped just now
Step 7: Important. Insert code<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/> to XXX.xib source code.
Warning: Do this before open it as Interface Builder! Otherwise, you will see wrong size and layout waring.
[![step 7][9]][9]
Step 8: New XXX.swift to connect the XXX.xib
[![step 8][10]][10]
Step 9: Add the view anywhere we want
[![step 9][11]][11]
I get warning: "You need at least 10 reputation to post more than 8 links."
Can you support me to upload the remaining 3 screenshots?

UISegmentedControl underneath UINavigationBar, WITHOUT scrolling

Ok so ive been at this problem from a day or so now, seems to be a fairly common problem amongst new iOS devs. However, ive search google, appledev, stack overflow etc.
However, ive had trouble finding the correct solution. No where has clear answers.
I need the segmented control to either change the tabledata source or show a new view entirely, either would be fine.
At the moment, i have a work around solution where i put the segmented control inside a tableheader, this would work fine, but i dont want the segmented control to scroll with the table cells.
Im trying to have it static and unscrollable, just like the Navigation bar.
Ive tried a few option in interface builder but neither of them work, either they dont show up or i get a crash because it couldnt find the table view.
Although, i do know it has something to do with putting a view inside a view?
If anyone has a spare moment to write a quick but detailed guide, i would be very thankful.
edit
Attempt 1:
in IB...
View
-> Tableview (tableview inside the parent view)
creates this error
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UITableViewController loadView] loaded the "EventsViewController" nib but didn't get a UITableView.'
First of all you will need a single controller which will contain the segmented control, table view and other elements which you want to add.
Since there is a single controller for your application, any change in the segmented control can be captured in it. Once you capture the event you can decide whether to reload the tableview by updating the tableview or load a new view.
This new view can also be loaded as a modal view or as a new view to a navigation control or as a subview to the current view. (the call is left to you).
The advantage will be the segmented control and table view will be part of a UIView and scrolling one will not scroll the segmented view out of sight. Other advantage will be the new view will be loaded on top of the current view. You can have delegate methods to handle the back press event on the new view. You can also get data from the new view to a single controller.
UIView
--- Segmented control(you can wrap this into a seperate UIView
--- UITableView (it can also be wrapped into a seperate uiview )
--- Other widgets
Now you can have a single view controller controlling the above Main UIView. Hope this helps you. I have used a similar approach in one of my apps.
What you can do is have a segmented control basically change the table data source. Basically, set the action hitting either segment as [myTableView reloadData];. Then, in your data source methods, put some if statements up. For example, under numberOfRowsInSection, say: if (mySegmentedControl.selectedSegmentIndex = 1) //2nd button, use this array. Get what I mean?
This way you can use one view controller to with basically two (really one) table.

iPhone - UITableViewController and Core Data rows repeating in tableView

I am new to Cocoa and Core Data and I've encountered a weird problem. I successfully created the Core Data model, imported the data, made the UI (Navigation controller, tableViewController with searcDdisplayController) but now I'm stuck at one problem. If I implement a UITableViewController (with no UITableView in the nib file), the results fetched from fetchedResultsController are weird - the row count is correct (500 rows), but they are repeating themselves - only 8 different rows.
However, I was able to fix this problem by adding #synthesize tableView; in the .m file.
Then i encountered another problem - when i click on a row and push the details view, then click the back button on the Navigation Controller, the selected row in the tableView is still selected.
I hope I am being clear on what's wrong. Let me recap:
If I don't put the #synthesize tableView; on the top of the implementation file, the deselection of the row is working fine, but the results are wrong - 8 rows (out of 500) repeating in the tableView.
If I put the #synthesize in the file, the data in the tableView is correct, but there is no animation when i get back to the tableView with the navigation controller.
I also tried to put a UITableView in the nib file of the listviewcontroller, but the results were the same - no deselection of the row. I also tried to deselect row in the viewWillAppear delegate, but the indexPath of the indexPathForSelectedRow is null.
Oh, and I'm using an NSFetchedResultsController. Like I said - the fetched results are correct, but it seems that they're not properly fed to the tableview (if it is not synthesized..)
Thanks!
I might be able to solve your row highlighting issue but you will need to include some code examples so we can help you with the other items...
To deselect a row you can call the method [deselectRowAtIndexPath:animated:] this will allow you to deselect the row before or after you push your detail view controller from your tableView selection delegate. Table View Reference
Thanks for responding!
Hmm..I may have found a solution, but I'm not sure it's correct.. I am implementing the UITableViewController in my new class, but I'm actually using a new UITableView (tableView2) for the data fetching. In the nib file i created the UITableViewController, set the Class name to my custom class BUT I didn't connect the Table View in the Interface Builder with my newly created tableView2. I hope you'll be able to understand.. It's quite complicated, this whole stuff :S
So if I get this right - if I implement an UITableViewController, my class automatically gets a self.tableView? But, like I said in my question, if I don't synthesize the property in the .m file, the controller can't deselect the row automatically..but if I do, the data isn't correct and is repeating..Maybe there is a problem in the cellForRowAtIndexPath, where i get my cells from self.tableView..but It's not logical :S
Anyway, now the row de-selection animation works without my interference and the data displayed is correct. I'm suspecting that the UITableViewController has some other methods that need to be overwritten if using the NSFetchedResultsController.
Do you think my method is incorrect? Having a new UITableView in my custom UITableViewController class?

how to manage a stack of UITableViews without a navigation controller

I am new to iPhone development, and I am working on modifications to an existing iPhone app. The high-level overview of the issue is this.
I have a button displaying a pop-up containing a UITableView.
I'd like to click on a menu item and display a second UITableView with sub-items, including a back option. If the user clicks back, they go the original UITableView. If the sub-item has additional sub-items underneath it, it should (when clicked) launch another UITableView with these options. There is also a back button as a menu item that will allow the user to navigate to the previous menu displayed.
The challenge here is that I am not using a navigation controller. the original developer only wants to add UITable Views to the stack, add transitions between them as you go from one menu to the other. Most of the tutorials I have seen and tried utilize a navigation controller and Interface Builder to associate the UITableViews.
Right now, I have an XML data source populating the menu, and when I click on a menu item, the titles change correctly, but still uses the same UITableView to display the options - this has consequences of course, as some of the sub-items may not fit on a screen.
any thoughts on how this can be done? I can post some code if necessary, although I think the general description should be able to ring a bell with one of you smart guys!
This can be done in numerous ways.
I haven't done this first one, but you can probably create a UINavigationController and set its view to the appropriate frame (inside the bubble) hide the navigation bar and set the action of your back button to pop the current view controller.
Another method is to have multiple tableviews on one controller, the delegate and datasource methods have the UITableView as an argument so you can distinguish them when setting the height of your rows, headers etc and when returning a UITableViewCell.
The way I've chosen to deal with such configurations is to have one UITableView and have only the datasource change. When you reload, insert, delete rows or reload the whole table, you can change anything you want depending on the current datasource level. The easiest none animated way is to reload the whole table.
a) If your "options" go off-screen height wise (you want fixed height) table change the - (CGFloat)tableview:(UITableView *)table heightForRowAtSection:(NSInteger)section return value
b) If your "options" go off-screen length wise either make your cell's default textLabel flexible: cell.textLabel.adjustsFontSizeToFitWidth = YES; cell.textLabel.minimumFontSize = 14; or have custom cells (lookup subclassing UITableViewCell, which is recommended) for each datasource level.
If you subclass TableViewCells remember to have different dequeue cell identifiers for each level, so the table doesn't provide you with another level's cell class.
For the "stack" of tableviews or datasources, you can have an NSMutableArray with addObject for push and removeLastObject for pop.
For animations, UITableViews can animate their rows/sections for 3. (see documentation for insert, delete, reload - Rows/Sections UITableView class reference), for 2. you can even have UIView (if not CoreAnimation as Grimless suggested) animations, that move the current tableview to the left (setFrame:) and the next tableview from the right (setFrame offscreen before animation and then in place in the beginAnimation-commitAnimation clause), but make sure you add the tableviews in a container view (the bubble interior) that clips its subviews.
Oi. This is gonna be a tough one. My suggestion would be to maintain your own stack implementation. Then, use CoreAnimation to add/remove UITableViews from your main view controller to get animated effects. So whenever the user clicks on an element in the current table view, the appropriate controller creates a new controller and table view, and then your custom navigation controller pushes the old one onto the stack, removes the old table view from the main view, sets the new controller as the current one, and adds the new table view to the main view. Kinda messy, but I think it will work.

Best way to handle multiple UITableViews in one UIViewController?

I have a UIViewController that should be capable of displaying tableViews with multiple data sources. The UITableView spans about half the size of the screen, and there is and up and down button that allows you to go through different data. Everytime the up and down button is hit, I'd like to ultimately use UIViewAnimationTransitionCurlDown or something similar to display the next UITableView.
The question is: do I need multiple UIViewControllers to do this, with a tableView embedded in each one? Should I just create one instance of UITableView and change its data source when an up or down button is hit? If it's only one instance of UITableView, how do I manage to get a curl transition over the portion of the screen it takes up to make it look like a new tableView is coming in?
Why not have each table view belong to its own UITableViewController, and nest these within the current screen's view controller? This way the screen's view controller is responsible for swapping out its subviews, each of which have a table view controller containing the necessary logic to show their data.
In the end, it comes down to what your functionality and data sets look like. It may end up being easier to implement the table view datasource & delegate code once, injecting an actual data source into this class - or it may be easier to write custom datasource code for each table view.