Yazzmi Yazzmi - 5 months ago 33
Objective-C Question

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?

Answer

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
}