views:

32

answers:

2

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?

+1  A: 

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.

Costique
A: 

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
}
Dan Ray
is it a good idea to cache this cell like with other regular cells? if so, how do you do that?
Yazzmi
You don't have to worry about caching this single cell because it's going to go away the minute you have actual data to show, and the autorelease will take care of its memory for you. It'll never scroll off the table, so there's really nothing to cache.By the way, you want to do this AS YOU LOAD your data, too. Then when your network process is done and you've filled your data source, call `reloadData` on your UITableView.
Dan Ray