Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling empty UITableView in UITableViewController

I have a UITableViewController that is populated with some data. If the data comes back empty, obviously the table is empty. What is the appropriate method to use to handle this case and put up something like a UILabel with "No Data Available".

I have been using - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section but it's proven a bit cumbersome and I'm not longer confident it's the best place to do this.

like image 802
Ternary Avatar asked Mar 22 '10 05:03

Ternary


People also ask

Why is there extra padding at the top of my UITableView?

Short answer is that this extra padding is probably due to the table view header (not the section header), and that UITableView doesn't like to be assigned a header with a height of 0.0.

How do I populate UITableView?

There are two main base ways to populate a tableview. The more popular is through Interface Building, using a prototype cell UI object. The other is strictly through code when you don't need a prototype cell from Interface Builder.

What is UITableView in Swift?

A view that presents data using rows in a single column. iOS 2.0+ iPadOS 2.0+ Mac Catalyst 13.1+ tvOS 9.0+


3 Answers

I'd probably reverse the philosophy a bit to only show the UITableView when content is available, and have the UIView show your 'no content' available image until the content is ready.

Once the content has been fetched and is ready to be presented - animate or create the UITableView into the view hierarchy over the 'no content' image and ask it to reload its data. This will have it start working through it's datasource delegate callbacks.

At least this way you won't have to worry about showing a tableview with no data, and mixing concerns with UITableViewDataSource callback methods.

like image 190
crafterm Avatar answered Oct 20 '22 18:10

crafterm


I just hid the tableview when there was no data instead. Simply subclass UITableView and override reloadData. You can also show/hide a text label appropriately to show no data available.

- (void)reloadData {
    [super reloadData];
    BOOL dataPresent = FALSE;
    int sections = [self.dataSource respondsToSelector:@selector(numberOfSectionsInTableView:)] ? [self.dataSource numberOfSectionsInTableView:self] : 1;
    for (int i = 0; i < sections; i++) {
        if ([self.dataSource tableView:self numberOfRowsInSection:i] > 0) {
            dataPresent = TRUE;
            break;
        }
    }
    self.hidden = !dataPresent;
    return;
}
like image 34
Shawn Avatar answered Oct 20 '22 17:10

Shawn


Why not just change the backgroundView property of the UITableView to something appropriate when the table is empty? You can update the backgroundView property anytime you load or reloadData for your tableView. The backgroundView is automatically sized to the size of your tableView and you can customize it to have whatever pictures, text, controls you want for the empty case.

like image 23
Ray Fix Avatar answered Oct 20 '22 19:10

Ray Fix