bendu
bendu

Reputation: 391

placeholder for empty uitableview section

After lots of searching, I can't seem to find what I'm looking for.

I have a UITableview where some of the sections may be blank to begin with. Here's a picture to help get an idea of what I'm talking about. I want to have some TEXT (not a table cell) in the middle between the footer and the header. Is there anything that I may have overlooked?

empty section

Upvotes: 1

Views: 2406

Answers (2)

K S
K S

Reputation: 212

What I did was create a UILabel with the same size as the tableview and add it to the tableview, something like:

UILabel* emptyLabel = [[UILabel alloc] init];
emptyLabel.textAlignment = UITextAlignmentCenter;
emptyLabel.backgroundColor = [UIColor clearColor];
emptyLabel.frame = self.tableView.bounds;
emptyLabel.text = @"Empty";
[self.tableView addSubview:emptyLabel];

You can then use the hidden property to show it or hide it, e.g. emptyLabel.hidden = TRUE;

Upvotes: 2

andycam
andycam

Reputation: 1692

Because of the nature of UITableViews, I'm not sure you could achieve replacing the UITableCell view with something else. However there's no reason you can't completely alter the table cell itself to look like a plain UITextLabel and not a cell! You could do something like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    /* Initial setup here... */

    if (thisCellHasNoDataYet) {
        // Prevent highlight on tap
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
        cell.backgroundColor = [UIColor clearColor];
        cell.textLabel.textColor = [UIColor blackColor];
        cell.textLabel.text = @"TEXT YOU WANT THE 'CELL' TO DISPLAY";
        // etc...
    }
    else {
        // Otherwise we have data to display, set normal cell mode
        [cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
        cell.backgroundColor = [UIColor whiteColor];
        // etc...
}

The benefit here is that once your condition is met, you just have to set the boolean (I used thisCellHasNoDataYet) to TRUE and call reloadData on your table!

Upvotes: 1

Related Questions