Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView No Data Screen

My UITableView get data off the internet. Sometimes it doesn't receive any (cell) data. It just shows a blank table.

How can I show a message/cell to the user that no data records have been found?

like image 483
Yazzmi Avatar asked Jul 20 '10 07:07

Yazzmi


3 Answers

You want to return one cell reporting on the lack of data.

If you're keeping your cell data in an array that's a class property (let's say NSArray *listings), you can go:

-(NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
    if ([self.listings count] == 0) {
         return 1; // a single cell to report no data
    }
    return [self.listings count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.listings count] == 0) {
        UITableViewCell *cell = [[[UITableViewCell alloc] init] autorelease];
        cell.textLabel.text = @"No records to display";
        //whatever else to configure your one cell you're going to return
        return cell;
    }

    // go on about your business, you have listings to display
}
like image 105
Dan Ray Avatar answered Oct 09 '22 02:10

Dan Ray


You can add a test in your -tableView:cellForRowAtIndexPath: implementation and display a special UITableViewCell saying "no results available" when you get no data.

Another approach, which generally looks better, is to add a custom view to the UITableView directly. Don't forget to remove it when there are results to display.

like image 27
Costique Avatar answered Oct 09 '22 02:10

Costique


You can add a UILabel to your view This can be created programmatically or in your xib. If your xib contains a UITableView as [self view], as is typical for a UITableViewController, then create a property for the label on your viewController

@property (nonatomic, retain) IBOutlet UILabel       *noResultsLabel;

Drag a UILabel from the library into the document window as a sibling to your "Table View". Set text properties (36pt bold font, light gray looks pretty good)
Control drag from "File's Owner" to wire the outlet to the label.

UITableView with no results UILabel

In your server callback, you can add the label to the view. Here's an example:

#pragma mark -
#pragma mark Server callbacks
- (void)serviceExecutionComplete:(NSMutableArray *)results {
    [self setServerData:results];
    if ([results count] == 0)
    {
        // no results,
        CGRect viewFrame = [[self view] frame];
        [[self noResultsLabel] setFrame:CGRectMake(0, (viewFrame.size.height - 100) / 2, viewFrame.size.width, 50.0)];
        [[self view] addSubview:[self noResultsLabel]];
    }
    else
    {
            [[self noResultsLabel] removeFromSuperview];
        [self.tableView reloadData];
    }
}

You could do the same thing in tableView:numberOfRowsInSection: if that fits your design better

like image 36
levous Avatar answered Oct 09 '22 03:10

levous