I want to display a text to user to inform that there is no content in the data source. For example you can see this kind of behavior in contacts App if you don't have contacts it says it at the middle of the table with gray text. Is there easy way to do this or do I have to use some trick like creating empty cells or something.. Th开发者_如何学Canks!
There isn't (AFAIK) any built-in way to have a table view display a message when empty. You could have tableView:numberOfRowsInSection:
return 1 if the datasource is empty and tableView:cellForRowAtIndexPath:
return a "dummy" cell saying there is no data. Or you could just add your own view to display the appropriate message, ignoring the (empty) table view completely.
Here's a short and sweet solution.
First step, in your UITableViewController initialization, 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.
Second step, in your UITableViewController, add:
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
self.placeholder.frame = self.tableView.bounds;
}
Last step, show/hide the placeholder when needed.
A better solution would be not using UITableViewController. Just add a tableView to a UIViewController and add a placeholder view to the UIViewController above the tableView. Use Auto Layout to have the placeholder centered vertically and horizontally. Finally, show/hide the placeholder when needed.
What do you guys think about that solution? Please comment.
In Swift 2.0
just below the calendar , I have a tableview
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)
}
I wrote up three different approaches in my blog:
- go with the simple solution described by Anomie: use conditionals in your datasource methods.
- use a "magical" do-it-all category UITableView+NXEmptyView.
- use a separate datasource class for special cases (empty, loading, …).
Please look here
I can suggest to pay your attention at DZNEmptyDataSet, StatefulViewController, UIEmptyState and StatusProvider
But this libs are big and in most cases you can use @Pablo Ruan's answer
精彩评论