Sathiya
Sathiya

Reputation: 429

how to display a table with zero rows in UITableView

I am loading a content into UITableView dynamically. If there is data the table needs to display the data. If there is no data the table should display a plain page. But in my application the table displays the plain page with two separator lines. i need to remove this separator line and display a plain white page. Please Suggest?

Any help would be appreciated!

Upvotes: 10

Views: 5670

Answers (6)

keep on you
keep on you

Reputation: 310

The good UI Design to add a new UIView to the UIViewController when there is no data "No Data available", by adding it will hide separator lines of the UITableView

Upvotes: 0

Diego Acosta
Diego Acosta

Reputation: 1737

It can be simplified to a single line, using @bbarnhart solution:

myTableView.tableFooterView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];

Upvotes: 1

Jiejing Zhang
Jiejing Zhang

Reputation: 1050

Two steps:

add this code in viewDidLoad

- (void)viewDidLoad
{
    emptyplaceholder = [[UILabel alloc] initWithFrame:CGRectMake(80, 100, 640, 100)];
    emptyplaceholder.text = NSLocalizedString(@"No shift changes...", "no shift changes");
    emptyplaceholder.alpha = 0.2;

    [self.view addSubview:emptyplaceholder];

}

add this code in controllerDidChangeContent:

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
    // The fetch controller has sent all current change notifications, so tell the table view to process all updates.

    if (self.fetchedResultsController.fetchedObjects.count > 0)
        emptyplaceholder.hidden = YES;
    else
        emptyplaceholder.hidden = NO;

    [self.tableView endUpdates];
}

so the UILable will disappear when have date cell, when delete to no data in table, the label will show again.

Upvotes: 0

bbarnhart
bbarnhart

Reputation: 6710

If you provide a view to the footer then the separators between the empty rows will disappear.

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
myTableView.tableFooterView = view;

Upvotes: 26

PeyloW
PeyloW

Reputation: 36752

A plain UITableView will always display separator lines even if it is empty. You might notice that if you have a list with only one or two rows, there are still separator lines visible. This is the default behaviour and should not be overridden unless you have a really good reason.

I am guessing you see only two separator lines because your rows are very tall?

If you do really need to remove the separator lines then set the separatorStyle property on your table view to UITableViewCellSeparatorStyleNone. Take note that this will affect all rows. So if you still want separators for the rows that do exists, then you must draw these separator lines in your own UITableViewCell objects.

Upvotes: 2

klaaspieter
klaaspieter

Reputation: 2665

You could check for the empty case if your datasource is empty add a placeholder view on top of the tableview. If they are not empty, remove the placeholder view.

Upvotes: 2

Related Questions