iPhone: UITableView Becomes Invisible after Changing the Data it Displays - iphone

I have application with a TabBar that controls several views. In one view, I control connections to different servers. Each server provides a different set of items.
I display these items in UITableView on another view.
The problem is that the tableview displays OK the first time but if I go back to view number one and change the server, thereby changing the list of items that should be displayed in tableview, the tableview becomes invisible for some reason. If I tap on the screen in the place where it should be, it becomes visible again.
I create table view like this
UITableView * aTableView = [[UITableView alloc] initWithFrame:CGRectMake(X,Y,Width,Height) style:UITableViewStyleGrouped];
[[self view] addSubview:aTableView];
aTableView.dataSource = self;
Ive tried to call reloadData and setNeedsDisplay in viewWillAppear of the UIViewController that hosts this tableview but without success.
Any advice?
Thanks

i don't know it will solve your problem or not but you are missing
aTableView.delegate = self;
or you haven't paste it here

I would guess that the problem is that tableview's datasource is not providing the data the first the table reloads after switching servers. If the datasource does not provide any rows, the tableview appears completely blank. That sounds like your issue.
I would look at the code where you switch servers as well as at the tableview:cellForRowAtIndexPath: Set a breakpoint/log there to activate after you switch servers and see what data the datasource is providing immediately after the servers switch. I think you'll find the table has no data. When you touch it, the table forces an update and by that time, the data has arrived.

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 use UITableViews for multiple datasets

I am currently playing around with a navigational based app. Where I want to allow the user to construct a search query by selecting one of several different uitableview cells when touched leads them to a sub uitableview where I will display the data for the user to select.
each cell will load the same subview however it will load it with different datasets. I am wanting to know an appropriate way of handling the transition of data (when the user selects the cell from the subveiw how can I control which cell that data should be sent back to?
I am thinking about passing the subview the Indexpath of the mainviews selected cell.. then passing it back to when the subview is poped from the stack so that it knows where the data needs to be.. is that the best solution? or is their another way of doing this?
Yes, give the subview a property for the indexPath of main view's selected cell. Set this property in the main view's didSelectCell method before pushing the subview.
Once you pass that indexPath back to the main view with you data, you can use
[self cellForRowAtIndexPath:indexPath]
to access the correct cell.

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

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.

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.

iPhone - UITableView background image loads only on every SECOND build

This is driving me utterly insane.
I'm building an iPhone app and in my root view controller I have a UITableView.
In viewDidLoad I've set a background image for this table like so:
[self.view setBackgroundColor:[UIColor blackColor]];// this is so that I can see when the table background image does not show up.
UIImageView* backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"whiteBackground.png"]];
[backgroundView setFrame:CGRectMake(0, 0, 320, 460)];
[self.table setBackgroundView:backgroundView];
[backgroundView release];
My tableCells are transparent (or have background images themselves).
It works every second time I build the app and run it, both on the simulator and on an actual device.
It will show up fine, then when I build again it will show just the black background (but the table calls have their background images/transparency).
Has anyone ever experienced anything like this before? The fact that it is being picked up and displayed every second build makes me think there is something lurking in memory.
When I run it on a device/simulator and then quit the app normally (double tap home, hold the icon and tap closed) and then run it again it works fine. It seems to be something to do with killing the app when I build it each time. Note that I use the same exact code on another viewcontroller and it works every time, but it doesn't work on this - my root view controller.
Do you see the same if you move this code to viewWillAppear instead?
FWIW it would be better to set your backgroundView.frame to self.table.bounds rather than hard coding it to the size of the screen.
I've been banging my head against a wall to fix this and have found out a few things (as mentioned in my reply to Andrew Ebling's response). It's more than just the background image.
I had my project set up like so:
A UIViewController with a XIB.
Drop a tableView in the XIB.
Link up the Delegate and Data Source to the UIViewController.
Create a IBOutlet UITableView named 'table'.
Link that up in IB.
That has always worked just fine for me. But there is something weird going on with my 'table' IBOutlet being recognised 50% of the time. Every second time I build it is not recognised and reloadData is not run whenever I call it. It loads the table first time (meaning the datasource is intact) and cells can be selected (meaning the delegate is fine) but any selectors sent to 'table' are ignored.
This only happens on the RootViewController. Not on any subsequent view controllers in which I use this way of doing things.
So I just changed the class of my RootViewController to UITableViewController, changed the XIB accordingly (replace the uiview object with a uitableview and set the file's owner view property to the UItableview). Basically I just reverted back to what XCode gives you when you specify a new file with a UITableViewController subclass and a XIB for user interface.
That way I use self.tableView to reload instead of my own IBOutlet and it works fine.
I have no idea what caused this though and would like to know if it's a bug or not.