iPhone: Adding a custom view to a UITableView Cell produces weird results - iphone

What I'm doing:
I have a custom ViewController called "MoreAppsViewController" - it displays/advertises several of the other apps I have created, and I use this same class/xib etc in several different apps. It controls a small view that gets displayed on options screens.
Normally I can just add this to any view with code such as:
MoreAppsViewController * moreApps = [[MoreAppsViewController alloc] initWithNibName:#"MoreAppsViewController" bundle:nil];
[self.view addSubview:moreApps.view];
-
The Problem:
I'm trying to display the same view in a UITableViewCell. This is the code:
moreApps = [[MoreAppsViewController alloc] initWithNibName:#"MoreAppsViewController" bundle:nil];
[cell.contentView addSubview:moreApps.view];
It does get displayed. However, some elements of moreApps.view appear lower than they should - they get drawn below the cell, but the rest of the elements get drawn in the cell correctly. Specifically, it's a scroll view that forms the main part of moreApps.view that gets pushed below where it should be (the scroll view and anything on it).
Any idea why this is happening or how to fix it? Any help would be much appreciated!
-
Lastly - The scroll view is just part of the .xib file, I don't do anything weird in the MoreAppsViewController either.
EDIT:
To clarify, by 'below' I mean further down the page. If I increase the cell's height, the scroll view is just pushed further down the page.
EDIT 2:
Here's what it was doing: (the bit below the cell should be appearing inside it)

Weirdly enough, placing the scroll view inside a dummy view fixes the problem (via the interface builder). Don't ask me why - feel free to explain though!
Seems to happen for any scroll view on a .xib, when placed inside a UITableViewCell of above default height.

Related

iOS - resize UITableView height programmatically

I'm having a problem trying to programmatically resize the height of a UITableView hosted within a UIViewController, using iOS5 and Storyboards. The VC displays a master/detail record style with the UITableView displaying the detail records. Depending on the type of master record shown, a set of buttons may be needed at the foot of the screen. If the buttons are not needed then I want the UITableView to extend it's height to take advantage of the space available. I'm using the following code :
CGRect tableFrame = [tableListView frame];
if (blnApprovalRec == YES)
tableFrame.size.height = 127;
else
tableFrame.size.height = 170;
[tableListView setFrame:tableFrame];
This code is called whenever the master record changes, including when the screen first loads in viewDidLoad. The problem is that when the VC loads, the UITableView doesn't paint using the size specified - it just paints with the default size from IB. Once the user changes the master record so the table is reloaded then everything works fine and the size changes as required. I've tried forcing a repaint using setNeedsDisplay, setNeedsLayout and reloadData but none of these worked.
The problem is that when the VC loads, the UITableView doesn't paint using the size specified
This happens, when table view is loaded, but it's UI is not getting refreshed. Please verify if you have forcefully called in viewDidLoad or viewWillAppear.
Hopefully your this code is in seperate method:
CGRect tableFrame = [tableListView frame];
if (blnApprovalRec == YES)
tableFrame.size.height = 127;
else
tableFrame.size.height = 170;
[tableListView setFrame:tableFrame];
When the view appear initially, you may have the default selected value from master record.
You can set that value/instance in calling function in viewWillAppear.
Can you show method name and code for, how you are calling above snippets of code forcefully?
You can resize UITableView programmatically but you need to create UITableView programmatically too. Don't use Storyboard.
I had the same issue, just needed to move the code from
- (void) viewDidLoad
to
- (void)viewDidAppear:(BOOL)animated
This is tricky, its hard to resize things like this dynamically.
i would try "setNeedsLayout" And "setNeedsDisplay" for your table, and your screen to force a redraw.
Other than that, I would storyboard it to the minimum size and use code to expand it.
Generally apple doesn't like us doing this, your buttons should be drawn over the top of the view inside another view, if thats possible.
Sorry I can't be more precise but I have solved all these issues by mucking around and with hacks, and giving up on resizing things and doing re-designs. Please let me know how you go though :)
Make sure you are setting the frame after the table has loaded.
Which method do you call that code in?

How to put AdWhirl-banner fixed above TabBar?

My app consists of a TabBar and several TableViews. I want to have the AdWhirl-banner fixed just above the TabBar (only in the first TableView), but thus far I have not been succeeding.
Until now, I have implemented the following code into my TableViewController:
AdWhirlView *adWhirlView = [AdWhirlView requestAdWhirlViewWithDelegate:self];
[self.tableView addSubview:adWhirlView];
adWhirlView.center = CGPointMake(160, 342);
And it indeed shows the Ad i want to see, only it is partly covered by a section header (from the TableView), and when scrolling the Ad scrolls along.
How can I achieve that the Ad is both on top (in terms of top view) and at a fixed spot (above the TabBar)?
Any help is greatly appreciated!
You need to add it to the tabBar layer.
Try this:
[self.tabBarController.view addSubview:adWhirlView];
You may need to reposition it do it's not underneath the tabbar.
I also add a footer to the tables so they can be scrolled all the way up without the ad getting in the way.
You will likely need to create your own subclass of UIViewController, adding instances of both the AdWhirlView and UITableView to its view, either programatically, or in the nib file.
Anthoer, hackier way to do this would be to add the AdWhirlView as a header view in the existing table, but I'd opt for the first way. It will give you more control over how you want it to look and behave.
I'd think another alternative here would be to have a container UIView that contains both a UITTabBar which contains a UITableView. If you're on the right tab, you could just request an ad. If the request is successful, just shrink the tabbed view enough to make room for the ad at the top and insert it into the container UIView.

iOS: Add UIView to UITableView

I'm trying to add a UIView on top over the UITableView to mimic the iPhone Facebook style menu. I have it working fine by making the controller a UIViewController then adding a tableview however I am unable to make the menu a static menu unless the controller is a UITableView.
Is it possible to add a view ontop of a tableview and only make the tableview in the background scrollable without the view in the foreground scrolling?
Here is what I have with the subclass being UIViewController
But I am unable to make the tableview cells static via IB since it is not a subclass of UITableView Controller.
EDIT per NSJones Code:
It seems to be going somewhat in the right track. However the view still blocks the table. If I remove the view from the storyboard it will only display the table.
You can make a view hover the same way you make any real thing hover; Hold it up with something invisible.
Basically what you want to do is create a clear UIView (with user interaction disabled) that is the size of your view controller's view, and add it as a subview to your view controller's view property. That way it sits invisibly on top. then you can add a subview to that clear view and that subview won't move.
Edit:
It seems this nice clean approach won't work for you since you need your view controller to be a UITableViewController. The answer for this slightly more complex approach is to use a delegate method for UIScrollView which also works for UITableView. Apple has a fantastic demo of this concept in the WWDC2011 - Session 125 - UITableView Changes, Tips, Tricks video. If you can watch it I highly recommend it. The meat of this issue begins at about 36:10.
But to sum it up you implement the scrollViewDidScroll: delegate method. And handle the movement of the tableview by adjusting the position properties of the view. Here I am keeping an UIView property named viewToKeepStill still using this method.
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
// CGFloat stillViewDesiredOriginY; declared ivar
CGRect tableBounds = self.tableView.bounds; // gets content offset
CGRect frameForStillView = self.viewToKeepStill.frame;
frameForStillView.origin.y = tableBounds.origin.y + stillViewDesiredOriginY; // offsets the rects y origin by the content offset
self.viewToKeepStill.frame = frameForStillView; // set the frame to the new calculation
}
Instead of adding it as a subview of the table view, add it as a subview of the superview of the table view; that way it won't scroll.
So instead of this:
[tableView addSubview:viewController.view];
Do this:
[tableView.superview addSubview:viewController.view];
Assuming you want something that is visible full-time with the table, start with a view which contains both the menu view and the UITableView. Make the table smaller so it ends where the menu view begins. The table view can work with less vertical space.
If you have your UIViewController's view to be your table view then your table is going to span over the whole screen, so you won't be able to add anything on top of it.
Why not try the following:
1) create a new UIViewController
2) add a view on top where you want your menu
3) in the space left under just drag a table view from the component library
4) don't forget to set the 2 table view delegates to be your view controller class
that's about it?

iPhone Multi-View Rotation Hell

This is my first post on here and I'm very new to iPhone developing, so please bear with me.
I have an app which has a few view controllers and each view controller has a few nib files that it controls and some of the nibs have more than one view. There is a toolbar throughout the app, controlled by the root view controller.
After lot's of searching, I have got rotation working on every screen, except one. I haven't got them to load in the correct orientation, but I guess that's a different question.
In my nibs that have more than one view, I can select the autosizing options only on the original main view. On the views that I have added, which I load using insertSubview, I can select the fixed margin options, but not the width and height resizing options. I am using Xcode 4.
My 1st Question, is why I am unable to select the resizing options on the additional views?
Sorry, I had to delete the images I put in here, showing what I mean, to be able to post.
Anyway, that is not my main problem, I just want to know, out of curiosity, why I can't select the resize options. I have found a way around it in the code. In the function where I load the subview, I add [viewName setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
E.g to load the view that I have named shiftStart, I use:
- (IBAction)loadShiftStart:(id)sender {
[self clearView];
[self.view insertSubview:shiftStart atIndex:0];
[shiftStart setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
}
Now here is my problem. I have a nib file that has 3 views, view which is the main view that loads first, rotaStart, which loads as a subview when a button on the first view is pressed and shiftStart, which loads as a subview when a button on the first view is pressed. rotaStart and shiftStart are almost identical, with a title, a text block, a Yes button and a No button. The only difference is the text in the box, and which view is loaded if you click on the Yes or No buttons.
I have all the same resize and margin options selected, in Xcode, for the title, text block and buttons and as far as I can tell, the code that loads them is identical (see code below), but shiftStart rotates correctly and rotaStart does not.
- (IBAction)loadRotaStart:(id)sender {
[self clearView];
[self.view insertSubview:rotaStart atIndex:0];
[rotaStart setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
}
- (IBAction)loadShiftStart:(id)sender {
[self clearView];
[self.view insertSubview:shiftStart atIndex:0];
[shiftStart setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
}
Can anyone tell me why rotaStart is not rotating, when the phone is changed to landscape please?
Sorry this explanation has got a bit long winded. I'm not sure how else to explain it.
Thanks
For anyone else looking at this, with a similar problem, #longball solved it for me. Somehow I had managed to add a second view over the top of the base view, complete with Labels & Buttons. Deleting the extra view fixed the problem.

Is it possible to put one UITableView over another UITableView in InterfaceBuilder?

I know I can use codes to add UITableView one by one.
[self.view addSubview:tableview1];//
[self.view addSubview:tableview2];//
I hope to do the same thing in InterfaceBuilder, when I drag one UITableView onto another one, the new one always pushes the old one to the bottom, rather than just stays over the old one of UITableView.
Welcome any comment
Thanks
If you have a parent view that will contain your tableviews then you shouldn't have any problem. If you're trying to place them directly in a window then I could see a problem. If things aren't lining up the way you want you can always change their position via the Size Inspector or by nudging them with the arrow keys (shift-arrow key moves in 10 pixel increments).
Why anyone would want a tableview on top of another tableview escapes me.
Your parent view should be a subclass of uiviewcontroller and your xib should have the root view as a uiview and not uitableview.
I used two overlapping table views to show two different contents on the same view, which could be toggles using a segment switch. Apparently my client requirements were vague enough that I couldn't just filter out data like how the phone app displays all calls/missed calls list.