Placeholder text in table if data source is empty

I want to display text to the user to indicate that there is no content in the data source. For example, you can see this behavior in the Contacts application if you don’t have the contacts that it says in the middle of the table with gray text. Is there an easy way to do this or do I need to use some sort of trick, like creating empty cells or something like that. Thanks!

+4
source share
5 answers

No (AFAIK) any built-in way to display a table view when empty. You can have tableView:numberOfRowsInSection: return 1 if the data source is empty, and tableView:cellForRowAtIndexPath: returns a "dummy" cell saying that there is no data. Or you can simply add your own view to display the corresponding message, completely ignoring the (empty) view of the table.

+8
source

Here is a short and sweet solution.

The first step, in initializing the UITableViewController, add:

 UILabel *placeholder = [[UILabel alloc] init]; placeholder.font = [placeholder.font fontWithSize:20]; placeholder.numberOfLines = 0; // Use as many lines as needed. placeholder.text = NSLocalizedString(@"Your text here", nil); placeholder.textAlignment = NSTextAlignmentCenter; placeholder.textColor = [UIColor lightGrayColor]; placeholder.hidden = YES; // Initially hidden. [self.tableView addSubview:placeholder]; self.placeholder = placeholder; // You'll need a reference to the placeholder. 

The second step, in your UITableViewController, add:

 - (void)viewWillLayoutSubviews { [super viewWillLayoutSubviews]; self.placeholder.frame = self.tableView.bounds; } 

Last step, show / hide the placeholder if necessary.

A better solution would not be to use a UITableViewController. Just add a tableView to the UIViewController and add a placeholder view to the UIViewController above the table view. Use the automatic layout so that the placeholder is centered vertically and horizontally. Finally, show / hide the placeholder, if necessary.

What do you think of this decision? Please comment.

+7
source

In Swift 2.0

just below the calendar, I have a tableview

ignore calendar

 func TabelaSemDados(Array:NSArray){ if Array.count == 0 { tableView.tableFooterView = UIView(frame: CGRect.zero) tableView.backgroundColor = UIColor.clearColor() var label = UILabel() label.frame.size.height = 42 label.frame.size.width = tableView.frame.size.width label.center = tableView.center label.center.y = (tableView.frame.size.height/2) label.numberOfLines = 2 label.textColor = UIColor.grayColor() label.text = "No Data Text" label.textAlignment = .Center label.tag = 1 self.tableView.addSubview(label) }else{ self.tableView.viewWithTag(1)?.removeFromSuperview() } } 

called viewdidapper:

 override func viewDidAppear(animated: Bool) { TabelaSemDados(self.Agenda) } 
+4
source

I wrote three different approaches on my blog :

  • move on to the simple solution described by Anomie: use conditional expressions in your data source methods.
  • use the do-it-all UITableView + NXEmptyView magic category.
  • use a separate data source class for special cases (empty, load, ...).
+1
source

Look here

I can offer to draw your attention to DZNEmptyDataSet , StatefulViewController , UIEmptyState and StatusProvider

But these libraries are large, and in most cases you can use @Pablo Ruan's answer

0
source

Source: https://habr.com/ru/post/1342715/


All Articles