View added to superview disappearing when navigating tableview - ios

I have a tableView that navigates to a detail view when the user selects one of the tableView cells. This main TableView is created in a UITableViewController class. (MasterViewController)
I used StoryBoard to create the master-detail tableviews.
Within MasterViewController, I lay a tableView over top of the main tableView when the user selects the To button. This second tableView allows the user to select multiple values from this list.
In order to prevent this second tableView from scrolling on the screen when the first (main) tableView scrolls, I have added this second tableView to the superView of the MasterViewController view. ([self.view.superview addSubview:_toView];)
So, in MasterViewController, I add a TableViewController (_toController). I instantiate a UIView (_toView), add a background image to it (_toViewBG) and then add a TableView to it (_toTableView). I then add _toView (which contains this second tableView) to the superview of the MasterViewController view.
_toController = [[ToTableViewController alloc] init];
_toView = [[UIView alloc] initWithFrame:CGRectMake(10,10,263,247)];
_toViewBG = [[UIImageView alloc] initWithFrame:CGRectMake(10,10,257,232)];
_toViewBG.image = [UIImage imageNamed:#"toViewBackground.png"];
[_toView addSubview:_toViewBG];
_toTableView = [[UITableView alloc] initWithFrame:CGRectMake(20,30,220,180) style:UITableViewStylePlain];
[_toTableView setDelegate:_toController];
[_toTableView setDataSource:_toController];
[_toView addSubview:_toTableView];
[self.view.superview addSubview:_toView];
[_toTableView reloadData];
This approach keeps the second tableView from scrolling when I scroll the main tableView. The second tableView stays on top and in place as the main tableView scrolls.
If I select a cell in the main TableView, I navigate to a detailed view. When I return to the main TableView, I am unable to get the second TableView (_toView) to be displayed.
I am not sure how to tell the _toView to come to the front. I realize it is added to the superview of the MasterViewController view. From some experimentation, this superview appears to be a CALayer.
Can anyone suggest how I would get this _toView to display in front of the MasterViewController view?
Thanks in advance for any assistance.
Solved
I ended up just adding the second tableview as a subview of the MasterViewController view.
[self.view addSubview:_toView];
I then disabled scrolling on the main tableview while my second tableview is visible.
Self.tableView.scrollEnabled = No;
Seems to be working fine.
Tim

It may be possible to make this work by calling [self.view.superview bringSubviewToFront:_toView], but my advice would be to make MasterViewController a subclass of UIViewController rather than UITableViewController, and then add both of your UITableView objects as subviews of the root view, (rather than referring to superview).

Related

Can't place UIImageView behind UITableView in storyboard

I can't seem to position a UIImageView behind a UITableView on my Table View Controller. I'm trying to do this within the storyboard designer. Moving a UIImageView onto the View Controller just stacks the imageview onto the tableview. In previous versions of XCode I had a window that allowed me to change the stack order of views within a view controller. Is this still possible?? I've tried dragging the views around in the jump bar and this doesn't work. So how do I accomplish this using XCode 5?
Thanks!
Use UIViewController for parent of UITableView!
Or if you want to set Background of tableView, can use this code
self.tableView.backgroundView = [[UIImageView alloc] initWithImage:
[UIImage imageNamed:#"bg"]];
UITableView has a backgroundView property. You will need to set it in code.
In your example, you're probably using a UITableViewController in your storyboard. This means that the root view is a UITableView and you can't add a UIImageView as a subview to a UITableView and put it behind the superview. Doesn't make sense.
In a previous version that you've made, I suspect that you had a UIViewController with a UIView root view, which you added a UIImageView subview and a UITableView subview on top of that.
Every UIViewController is associated with a UIView, i.e. [viewController view]. In the case of UITableView, [viewController view] is of type UITableView, so when you add subviews to it, you're adding to the UITableView, not a superview of type UIView as you seem to expect.
I would recommend using a UIViewController to control that view, adding subviews of types UIImageView and UITableView.

UITableView as subview of UITextView not properly displayed

I'm trying to implement an autocomplete UITextView. The auto-suggestion is working fine. But the UITableView is getting clipped off. Please look at the image below.
The greybox is the actual UITableView. This UITableView is defined in another .xib file and is being called from another ViewController.
autocompleteTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 30, 320, 35) style:UITableViewStylePlain];
autocompleteTableView.delegate = self;
autocompleteTableView.dataSource = self;
autocompleteTableView.scrollEnabled = YES;
[self.textViewCell addSubview:autocompleteTableView];
here autocompleteTableView is the UITableView and textViewCell is the UITextView. And this is getting called from another ViewController which makes the autocomplete box to constrict to UITextView size.
What i want to achieve is something like this :
Your problem would appear to be because of the view you are adding the table view as a subview of. Even if the table view was visible it most likely wouldn't respond to touches because of the frame of the superview (taps outside the frame aren't handled).
You should consider doing something like adding a new view higher up the view hierarchy which replaces (overlays) the text field and adds the table view. This needs to be added high enough up the hierarchy that the host view is big enough to fully contain the new view (so, the view controllers view).
It should work if you handle the text field as you currently are but add just the table view at the top level of the view hierarchy.

tableView initwithframe and storyboard

I'm trying to add a tableview as subview to my tableViewController, but I want to setup the cells in storyboard. It will be a static tableview.
This is the code for calling the tableview on button click.
- (IBAction)botaoAdicionar:(id)sender {
AtividadesPraticadasTableView *tableview = [[AtividadesPraticadasTableView alloc] initWithFrame:CGRectMake(0, 170, 320, 320) style:UITableViewStylePlain];
[self.view addSubview:tableview];
}
In the tableview class I have this:
#implementation AtividadesPraticadasTableView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
Now, I have a viewcontrollerin storyboard with a tableview, which the class of the tableviewI changed to this file AtividadesPraticadasTableView. It has three static custom cells in storyboard, therefore it opens a blank default tableview.
What am I missing?
Static table views are entirely contained within the storyboard, and require information from the storyboard to display their content.
You've defined a static table view controller in the storyboard, populated it and set the tableView's custom class to your custom class, but when you want to add the table view you are just instantiating a table view of that class. That isn't going to get any of the information you've added to the storyboard.
In addition, the static cells information is read and understood by the UITableViewController, not the UITableView, so you are at the wrong level there too.
You need to do the following:
Get a reference to the storyboard, either from your original view controller's storyboard property (if it is on the same storyboard as your static table) or using storyboardWithName:bundle:.
instantiate the table view controller from that storyboard, using instantiateViewControllerWithIdentifier:. This will create a table view controller object containing all your static cells
Add this as a child view controller to your original view controller, using addChildViewController:
Add the table view controller's tableView as a subview
It may be simpler to add a container view in the storyboard to hold this view, and reveal it when the button is pressed, as Mike Slutsky suggested - this will do all of the instantiating and adding and child view controller-ing work for you, but the principle is still the same.
Also, adding a table view as a subview to a table view controller sounds very dodgy - a table view controller already has a table view as its view, and you can't really add subviews to that.
The thing your missing is the association between the programatically instantiated tableview and the UITableView that you put in your storyboard. You cannot just draw UITableViews in your storyboard and start instantiating new UITableViews in the controller's code and expect xcode to know which UITableView you wanted to use from the storyboard. Use an IBOutlet to connect a global variable for the controller to the UITableView in the storyboard, then the controller will know what you're trying to refer to. If you want that UITableView to appear on a button click, simply set the UITableView to hidden by default in the storyboard and then unhide it when the button is pressed.
The thing You are missing called manual. Check this protocol for TableView dataSource https://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html#//apple_ref/occ/intf/UITableViewDataSource
P.S. Here good tutorial for storyboards http://maniacdev.com/ios-5-sdk-tutorial-and-guide/xcode-4-storyboard

Adding Common instance of UIView at zero index of UITableView does not show

The structure of my classes is as fellow
I have a main view controller, let say MainVC;
Four different view controllers, say vc1, vc2,vc3 and vc4; Each vc has its own UITableView;
MainVC displays these VCs as subView;
A sub class of UIView, say headerVC, which returns me a header view which need to show as banner over each table in VCs; This view contains the buttons which switches the Vcs on MainVC;
Now when I run MainVC, it loads the view of VC1 (which has a tableView) and then VC1 adds the headerView at first row of its tableView; When I press any button over headerView, the MainVC toggles the VCs as per selection; Eech VC adds the headerView at first row of its tableView;
Its all works well, but when I navigate away to another tabbar item and comeback, the headerView disappears from selected VC's table; Rest of the rows displays as it is, but header view goes aways; When I scroll the tableView up/down the headerView (in first row) appears again; I'm not getting the reason why it behaving like this;
I tried to put headerView in both tableView's header and section's header but it didn't appear at all; By this approach atleast header view is displaying and working well, but now when I change the tab and come back, only the first row (which contais the header view) disappear;
any clue??
Remove this headerview from row of TableView and add as a static view to the VC, if you want it to move it with the tableView, you can do it via this code
float scrollStartPositionY;
-(void) scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView.contentOffset.y>=scrollStartPositionY)
header.hidden=YES;
else
header.hidden=FALSE;
header.frame =CGRectMake(header.frame.origin.x,TableView.frame.origin.y TableView.contentOffset.y- header.frame.size.height, header.frame.size.width,header.frame.size.height) ;
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
scrollStartPositionY=scrollView.contentOffset.y+5;
}

Insert image at top of UITableView when using UINavigationController?

I have a UINavigationController and UITableView in my MainWindow.xib. I'm trying to insert a non-touchable/non-movable image at the top of the view. (I don't want to simply change the UINavigationBar background, as from what I've experienced, the UINavigationBar height cannot be arbitrarily increased.)
As this is a root view, its behaviour is controlled by my RootViewController. When the view loads, I hide the navigation bar using self.navigationController.navigationBarHidden = YES;
I've tried adding a UIView to the top of the UITableView, but when the user scrolls through the table, the top image moves. How can I place a stationary image at the top of the UITableView without it moving as if it were a cell and without using a UINavigationBar background image?
Naively-Considered Possibilities:
Enclose the view in another view, the top of which contains the image?
Overlay an image mask at the top of the screen?
(HIG violation, anyone?)
If your view controller is a UITableViewController you can't easily add another view because the tableViewController just manages a single view which is the UITableView. I recommend using a plain UIViewController instead where you add a UIView for your top view and a UITableView for your content as subviews to the main viewController.view. Then, implement the UITableViewDelegate and UITableViewDataSource protocols in your UIViewController.
You can create another UIViewController which contains the UITableViewController and your static image on top. This way, you can even resize the table view so the static image is displayed above and not over your table.
UIViewController *middleViewController = [[UIViewController alloc] init];
[[middleViewController view] addSubview:tableView];
[[middleViewController view] addSubview:staticImageView];
...
[navigationController initWithRootViewController:middleViewController];
Has been a long time since I made my last cocoa application, so I cannot promise that it works.

Resources