10

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!

Sathiya
  • 429
  • 1
  • 6
  • 11
  • it's this easy ... [self.tableView setTableFooterView:[UIView new]]; – Fattie Nov 28 '13 at 19:05
  • Note that the actual solution is **simply to set it to be a "grouped" style table**! To do that, simply go to Thomas Decaux' answer here: http://stackoverflow.com/questions/1006663/how-can-i-set-a-uitableview-to-grouped-style – Fattie Nov 28 '13 at 19:11

6 Answers6

26

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;
bbarnhart
  • 6,620
  • 1
  • 40
  • 60
  • 1
    Note that `tableFooterView` is a retained property, so `view` should be (auto)released. – Ben Hoyt Nov 21 '11 at 18:40
  • Works perfectly. Couldn't be simpler. – n13 Mar 07 '12 at 09:55
  • 1
    Also just to add, you don't need to set frame here. `myTableView.tableFooterView = [[UIView alloc] init];` is good enough. – Byte May 28 '13 at 21:08
  • It can be simpler! :) **[self.tableView setTableFooterView:[UIView new]];** Also, why would you make it one pixel high? if (for some reason) you want to do it the "long" way just make it 0,0,0,0 – Fattie Nov 28 '13 at 19:07
2

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.

klaaspieter
  • 2,665
  • 1
  • 27
  • 32
  • Sounds like an ugly hack to me, and ugly hacks tend to break real functionality down the road. – PeyloW Sep 29 '09 at 14:42
  • I believe vice versa: there should be "something" to explain user that application is working as planned, it did not crash, it did already finish what it was doing and - most importantly - this space is empty on purpose, but only for now. – JOM Dec 29 '09 at 14:11
  • I don't see why this is an ugly hack, it's just a way of displaying a custom view while there is no data. The custom view can explain why there is no data and how the user can enter data. It's a common practice in iPhone (and Mac) applications. – klaaspieter Jan 04 '10 at 11:46
2

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.

PeyloW
  • 36,742
  • 12
  • 80
  • 99
1

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

myTableView.tableFooterView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
Diego Acosta
  • 1,737
  • 3
  • 16
  • 21
0

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

keep on you
  • 310
  • 6
  • 21
0

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.

Monolo
  • 18,205
  • 17
  • 69
  • 103
Jiejing Zhang
  • 1,030
  • 8
  • 16
  • Sorry, my model is a tableView controller with same date information. so the words should be: when the place holder UILabel will hidden when table view have "real" table view cell. – Jiejing Zhang Jan 21 '12 at 05:19