0

I have made a view in the application that shows a UITableView. It will be full of results (obviously) almost all the time, however when it does not have any results I want to show another view that inform the user about how he/she could populate the table.

I want to design that view in the interfacebuilder. I will have to check in the code whether the datasource is empty or not to toggle between the two different nibs. How do I instantiate and configure a view made in Interfacebuilder?

LuckyLuke
  • 47,771
  • 85
  • 270
  • 434
  • Please check this one [UIView and initWithFrame and a NIB file. How can i get the NIB file loaded?][1] [1]: http://stackoverflow.com/questions/5056219/uiview-and-initwithframe-and-a-nib-file-how-can-i-get-the-nib-file-loaded – DLende Jun 18 '12 at 07:53

3 Answers3

0

The easies way to do this is by adding the view in xib normally and make it visible Design your both views, the table view and the other view, give the tableView a tag of 111 for example and give the otherview another tag 222 for example

Now in viewDidLoad

Get both the views

UIView *noDataView = [self.view viewWithTag:222];
UITableView *tableView = [self.view viewWithTag:111];
//Hide both of them or only the noDataView until you know if you have data from the dataSource or not

Check for your data source

//hasElements do you have any element to show?
if(hasElements)
{
    noDatView.hidden = YES;
    tableView.hidden = NO;
}
else
{
    noDatView.hidden = NO;
    tableView.hidden = YES;
}
Omar Abdelhafith
  • 21,163
  • 5
  • 52
  • 56
0

You can load nib file based on condition.You can write category as follows:

self.view = (UIView *)[self loadNib:@"SecondView" inPlaceholder:self.view];

- (UIView *)viewFromNib:(NSString *)nibName
{
    NSArray *xib = [[NSBundle mainBundle] loadNibNamed:nibName owner:self options:nil]; 
    for (id view in xib) { // have to iterate; index varies
        if ([view isKindOfClass:[UIView class]]) return view;
    }
    return nil;
}

- (UIView *)loadNib:(NSString *)nibName inPlaceholder:(UIView *)placeholder
{
    UIView *nibView = [self viewFromNib:nibName];
    [nibView setFrame:placeholder.frame];
    self.view = nibView;
    //[self.view insertSubview:nibView aboveSubview:placeholder];
    //[placeholder removeFromSuperview];
    return nibView;
}
Nuzhat Zari
  • 3,398
  • 2
  • 24
  • 36
0

The other answers give you possible technical solutions but I would propose that if you are using the standard Apple design guidelines, you probably don't even need to worry about it. For instance, somewhere on your screen you should have a bar button item with the identifier "Add" (which shows the plus icon). Then rather than giving a long (often poorly localised) description of how to add items, just have a header for an empty section which says "No items" replacing items with whatever pluralised noun is appropriate for your table's items. For example, for an Archery related app I am working on:

Empty Table View with obvious bar button items

Notice how the Edit button is currently disabled too, thus no explanation is needed as the only thing they can do at this point is tap the Add button (screenshots on the Appstore will have shown them what they can expect to see after this point).

brindy
  • 4,585
  • 2
  • 24
  • 27