I have an application that has a UITableView and a custom UIView on the same storyboard. The UIView loads a custom chart based upon the selection, and is loading a UIWebView with HighChart (the data and html is all locally stored, not being pulled over the network).
When I select the UITableViewCell, the view never displays. It loads the subviews (I'm NSLoging that out to console) and all the data is correct, but the display itself never refreshes. I've tried sticking [myCustomView setNeedsDisplay] at 20+ different places, but I'm starting to feel like it's something else.
Is it possible that there is an issue because I'm loading a web view? Is it essentially redrawing my UIView before the asynchronous call to my UIWebView is ready to do anything? What's the easiest way to test this?
Updated with some code (Skipping some, I'll fill in more as requested):
#implemention MainViewController
-(void)viewDidLoad {
***normal viewDidLoad stuff***
_myCustomView = [[MyCustomViewClass alloc] initWithFrame: CGRectMake(0,_tableview.frame.origin.y + _tableview.frame.size.height,_tableview.frame.size.width, 176)];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
MyCustomJsonDataObject *dataObject = [self.jsonDataArray objectAtIndex:[indexPath row]];
[_myCustomView redrawWithDataObject:dataObject];
}
#end
#implementation myCustomViewClass {
UIScrollView *scrollView
}
-(void)redrawWithDataObject:(MyCustomJsonDataObject *)dataObj {
scrollView = [[UIScrollView alloc] initWithFrame:etc];
// THere are 3 screens initialized: 1 chart data with a UIWebView, and two custom UIViews.
scrollView.contentSize = CGSizeMake(scrollView.size.width * 3, 176)
** initialize web view from custom class **
[scrollView addSubview:webView];
** initialize custom view from custom class **
[scrollView addSubview:UIView1];
** initialize custom view from custom class **
[scrollView addSubview:UIView2];
** set some scrollView properties (scrollable, paging, etc) **
[self addSubview:scrollView];
[self setNeedsDisplay];
}
The custom UIWebView class takes data from the JSON object, and replaces javascript placeholder variables in a local html file, then feeds the new file back into the webView. The others two views are just a bunch of simple labels that populate data from the custom data object.
I figured out my issue:
_myCustomView was already being defined in the storyboard, so it was initializing itself. When I called the init method in my viewDidLoad, it appears I was adding a SECOND instance of the view, overwriting the first, which is why it displayed blank and didn't appear to refresh. When I removed the init method for the view in viewDidLoad, it worked without issue.
Related
So I have an application that builds the UI from a storyboard, and I have this ViewController that needs to render both a UITableView and UICollectionView within the same scene.
So I basically created a regular UIViewController that that contains both a UITableView and a UICollectionView. The views are obviously set within the storyboard. The scene ViewController then contain a both UICollectionViewController and a UITableViewController.
I went this way cause this specific collectionView is a reuse from a very similar collection view from another place in the app. It works perfectly fine there, however, here nothing happens and the view doesn't appear there on run time. The tableView works just fine, and from what I've found so should the UICollectionView, but obviously I'm missing something. Is there something extra that a regular UIViewController needs to have (a method or a property) so that it will display the UICollectionView? Obviously I'm missing something since everything I've found about this indicates that it is possible to do it this way.
Edit:
Relevant Code in viewDidLoad:
self.productsCollectionViewController = [[ContentPageCollectionViewController alloc] init];
self.productsCollectionViewController.collectionView = self.collectionView;
[self.collectionView setBackgroundColor:[UIColor cyanColor]];
[self.productsCollectionViewController setSectionSizes:#[[NSNumber numberWithInteger:7]]];
self.filtersTableViewController = [[ProductsFilterTableViewController alloc] init];
self.filtersTableViewController.tableView = self.tableView;
[self.tableView setBackgroundColor:[UIColor yellowColor]];
I had this question when/where to create and initialize views that are created programatically, so I hope some discussions here will shed more light on this topic for me.
This slide:
says: "not to initialize something based on the geometry of the view in viewDidLoad" and suggests viewDidAppear.
Imagine my view controller has view. I want to add 10 dynamic UIButtons to it.
Shall I put the code like below to the viewDidAppear?
-(void) viewDidAppear:(BOOL)animated
{
...
UIButton *button1 = [[UIButton alloc] initWithFrame: rect1];
[self.view addSubview: button1];
UIButton *button2 = [[UIButton alloc] initWithFrame: rect2];
[self.view addSubview: button2];
...
}
But this creates the buttons each time the view is shown. Is it what we want?
On the other hand if I put the code in viewDidLoad slide suggest not to initialize geometry of these views there.
Or shall we create buttons in viewDidLoad and set their frames in viewDidAppear?
What approach do you usually take?
But this creates the buttons each time the view is shown. It's true.
So the best thing you can do is to add a boolean (lets name it isLaunched). You set it to FALSE in the method -(void)viewDidLoad
Then add a if condition in your -(void)viewDidAppear where you perform creation of buttons (or other stuff) and set the boolean to true at the end.
You should have something like that :
-(void)viewDidLoad
{
//some settings
isLaunched = FALSE;
}
-(void)viewDidAppear:(BOOL)animated
{
if(!isLaunched)
{
//creating and adding buttons
isLaunched = TRUE;
}
}
zbMax (and now Amar) offered good solutions to implement the view creations in viewDidAppear: I will provide the rational for doing this (over viewDidLoad).
It is pretty simple actually. In viewDidLoad none of the views are actually setup yet, so any attempt to set/create frames or bounds will be extremely inconsistent. Struts and springs (or autolayout) will take effect after this method which will create additional changes to your views. viewDidAppear: is the correct place to do this because you can now rely on existing views and setting frames.
Reason for not playing with the geometry in viewDidLoad is because view is still in the memory and not on the window. Once the view is put on the window, then you can specify geometry. That happens when viewDidAppear is called for your controller.
As recommended, you should do all the initialisation in viewDidLoad as this is one time task and need not be repeated. Hold references to the added subviews and give them appropriate frame in viewDidAppear.
When you are dealing with custom UIView and its subviews, layoutSubviews is the method you need to override in the custom view in order to rearrange the geometry of its subviews.
Hope that helps!
I have to 'repeat' a uitableview into 2 or more view controllers. Like using a uitableviewcontroller with its methods inside view controllers using the methods/delegates/datasources from the uitablewviewcontroller.
Example: I've a uitableviewcontroller wich displays a twitter feed with its own style, so I need this table in more than 1 view controller and maybe just changing the twitteruser in each view controller (just an example).
TRY: What I've done is to create (with storyboard) a uiviewcontroller and a uitablewviewcontroller, both with their own methods. And on uiviewcontroller viewdidload, try to add the uitablewviewcontroller.tableview as a subclass. and this works! But the result is an empty table. I tried to set the delegates/datasources but it ddnt work..
-(void)viewDidLoad
{
[super viewDidLoad];
//Add Table as subview
thetable *t = [[thetable alloc] init];
[self.view addSubview:t.tableView];
t.tableView.frame = CGRectMake(0, 100, 320, 2000);
t.tableView.backgroundColor = [UIColor blueColor];
}
This code works, but just displays an empty uitable inside the view controller..
*thetable is a uitableviewcontroller object (.h, .m, and storyboard view)
*Just set the backgroundcolor to check if there's anything on screen
*using ios6
Thanks!
Make sure you are implementing the table view delegate and datasource methods in thetable.Also try [t.tableView reloadData] after adding the tableView to the view controller's view.
I'm using XCode 4.2 and have built my UI using Storyboards. I need to create a view that has content above and below a UITableView and I can achieve this by using a UIViewController. A UITableViewController does not let you add content above or below the table. You can use the table header/footer but that doesn't work for what I would like to achieve.
I now have a UIViewController with a UITableView embedded in it. I can adjust the height and width of the UITableView accordingly which provides me the UI layout that I am looking for.
I can customize the static cells in the UITableView but when I try to build I get the following error:
Illegal Configuration: Static table views are only valid when embedded in UITableViewController instances
My question is how are others getting around this? Creating a tableview with static cells and laying them out visually is very nice but apparently that is not allowed for some reason that I cannot understand. I can't switch to a UITableViewController because of my visual layout requirements.
Any assistance would be greatly appreciated.
You can achieve this in Xcode 4.5 and later versions, assuming your app is targeted at iOS 6+.
In the Storyboard simply create a UIViewController with a View Container inside it's main view. Then hook up that View Container to a UITableViewController that contains static cells.
Just like this:
You don't need a single line of code. Just control click, drag and select embed. The view controller containment is handled for you.
You are right. In storyboard, you cannot have a tableView with static cells embedded in a viewController. One way around it (I have not tried it myself, though, so I am not sure if it works) can be that you create an instance of UITableViewController in storyboard with static cells. Add an instance of UIView to your viewController, and then programmatically load the tableView of the UITableViewController into the UIView of your viewController.
pmd's answer works but in the event that backward compatibility with iOS 5 is required as well, you can do the embedding programatically using the View Containment API.
In the viewDidLoad method of your parent UIViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
MyTableViewController* vc =[storyboard instantiateViewControllerWithIdentifier:#"MyTableVC"];
[self addChildViewController:vc];
[self.view addSubview:vc.view];
// ensure embedded view is aligned to top
CGRect frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
vc.view.frame = frame;
[vc didMoveToParentViewController:self];
}
Don't forget to specify a storyboard ID for your UITableViewController with the static cells.
I know this is an old question but I have a scrappy solution to this issue.
I needed 3 static cells on a UIViewController so this is what I did:
Drag in some Table View Cells into your interface (not into UITableView) - add text and whatever else you need.
Make IBOutlet properties for your cells and synthesise them.
Drag a button and make it cover the entire cell. Set the button to type 'Custom' so it appears invisible - repeat for all cells
Add numeric tags to your buttons
Implement the following functions. buttonDown is connected to the buttons 'Touch Down' event. buttonUp is connected to 'Touch Up Inside' AND 'Touch Up Outside'
-(IBAction)buttonDown:(id)sender {
if ([sender tag] == 1) {
myFirstCell.selected = YES;
}
else if ([sender tag] == 2) {
mySecondCell.selected = YES;
}
else if ([sender tag] == 3) {
myThirdCell.selected = YES;
}
}
-(IBAction)buttonUp:(id)sender {
myFirstCell.selected = NO;
mySecondCell.selected = NO;
myThirdCell.selected = NO;
}
You can do whatever else you like in the buttonDown event and use the button to go directly to a new view. I find this pretty useful.
I am not sure what you mean by static cells, but if you are trying to build the cells in IB, and then want to use it in your tableView, what you could do is in your cellForRowAtIndex you can call loadNibNamed passing the name of the .nib file you created for the cells as the parameter. Make sure that you have an outlet in your viewController which maps to the cell's .nib. Try exploring in these directions if that's what you are trying to achieve
You can make it dynamic and then switch of scrolling:
[yourTableName setScrollEnabled:NO];
If we need to add multiple views say - a tableview, a mapview and 2 or 3 more views on a single scrollable screen, then what would be most efficient way to do it ?
And will it be suitable for an app from the point of memory management?
Please share your thoughts.
Here's how you can do it programmatically: just add them in your viewDidLoad (or possibly your viewWillAppear) methods.
- (void) viewWillLoad
{
self.myScrollView = [[UIScollView alloc] init];
//Configure scrollview here (frame, contentsize, contentoffset...etc)
UITableView *table = [[UITableView alloc] init];
//Configure table here (frame...etc)
[self.myScrollView addSubview:tableView];
//Continue adding other subviews here
}
Or you can do it visually using storyboards. Just drag the views that you want onto your storyboard and then cntrl drag to your .m or .h files.
And yes, iOS is designed for displaying multiple views at once. It is suitable.