tags:

views:

51

answers:

3

I have a an app that has a UITabBarController. What I would like to achieve, is that the first ViewController included in the TabBar displays a TableView if there are items in the array property (loaded from CoreData), or a UIImageView (with more information about how to add items) if not.

If the user goes to a different TabBarItem, and comes back to the first TabBarItem the array could have been populated. So it should change what is displayed accordingly. Could somebody post a programatic snippet to achieve this. I have tried several things, and none have worked properly.

A: 

[self performSelectorInBackground:@selector(loadRecentInBackground) withObject:nil];

Then defining the selector

  - (void) loadRecentInBackground{

  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 

  // Define put the values into the data structure that you retrieve 
  // (I use an NSMutableDictionary)

  [pool release]; 

}

and retrieve the value from the dictionary in

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

and make sure to add the:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

where you return the count of the data structure.

David Sowsy
Hi David, I dont see in your answer any logic to switch to the UIImage View if the array is empty.
Benjamin Ortuzar
A: 

The easiest way would be to set a flag based on whether the array is populated or not. Then adjusting the table to display either rows of data or a single row displaying an UIImageView with the instructions.

It would be best to set the flag in -viewWillDisplay and the format the table by returning the correct values based on the flag from – tableView:heightForRowAtIndexPath:, – tableView:willDisplayCell:forRowAtIndexPath: and – numberOfSectionsInTableView:.

TechZen
Thanks for your answer, I would like to display the UIImageView instead of the TableView, rather than displaying it within the TableView.
Benjamin Ortuzar
Just to be clear, I meant that you could display a single cell that took up the entire screen so that the user couldn't tell that it wasn't an image.
TechZen
A: 

This code does the job

- (void)loadView {

    UIView* aView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];

    //tableView
    self.tableView = [[[UITableView alloc ] initWithFrame:CGRectMake(0,0,320,460) style:UITableViewStylePlain] autorelease];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight);                    

    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.autoresizesSubviews = YES;

    [aView addSubview: tableView];

    self.view   = aView;
    [aView release];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    [self loadData];

    //Display or remove Image depending on the ammount of results.
    if ([self.itemsArray count] == 0) {
        NSLog(@"No Items in Array");

        if (noDataImageView == nil) {
            noDataImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"NoDataBackground.png"]];
            [self.view addSubview:noDataImageView];
        }

    }else{
        if(noDataImageView != nil){
            [noDataImageView removeFromSuperview];
        }
    }

}

- (void)dealloc {
    [super dealloc];
    [tableView release];
    [noDataImageView release];
}
Benjamin Ortuzar
Is this assuming you're using a UITableViewController? I'm confused between the UITableViewController's view and tableView and when they're active.
Travis
This is a UIViewController. The view property of the viewcontroller has two subviews: tableView and noDataImageView. I prefer using a UIViewController and adding a tableview as a subview, its more flexible and versatile than a tableViewController.
Benjamin Ortuzar