I have a UITableView that in some cases it is legal to be empty. So instead of showing the background image of the app, I would prefer to print a friendly message in the screen, such as:
This list is now empty
What is the simplest way to do it?
I have a UITableView that in some cases it is legal to be empty. So instead of showing the background image of the app, I would prefer to print a friendly message in the screen, such as:
This list is now empty
What is the simplest way to do it?
Show Message for empty list, Wether its UITableView or UICollectionView.
Usages:
OR:
Remember: Don't forget to remove the message label in case data will come after refresh.
So for a safer solution:
and for
UICollectionView
as well:The easiest and quickest way to do this is to drag a label on to side panel under tableView. Create a outlet for the label and the tableView and add a if statement to hide and show the label and table as needed. Alternatively you can add tableView.tableFooterView = UIView(frame: CGRect.zero) this to you viewDidLoad() to give an empty table the perception that it is hidden if the table and background view have the same colour.
Using a Container View Controller is the right way to do it according to Apple.
I put all my empty state views in a separate Storyboard. Each under it's own UIViewController subclass. I add content directly under their root view. If any action/button is needed, you now already have a controller to handle it.
Then its just a matter of instantiating the desired view controller from that Storyboard, add it as a child view controller and add the container view to the tableView's hierarchy (sub view). Your empty state view will be scrollable as well, which feels good and allow you to implement pull to refresh.
Read chapter 'Adding a Child View Controller to Your Content' for help on how to implement.
Just make sure you set the child view frame as
(0, 0, tableView.frame.width, tableView.frame.height)
and things will be centered and aligned properly.I recommend the following library: DZNEmptyDataSet
The easiest way to add it in your project is to use it with Cocaopods like so:
pod 'DZNEmptyDataSet'
In your TableViewController add the following import statement (Swift):
Then make sure your class conforms to the
DNZEmptyDataSetSource
andDZNEmptyDataSetDelegate
like so:In your
viewDidLoad
add the following lines of code:Now all you have to do to show the emptystate is:
These methods aren't mandatory, it's also possible to just show the empty state without a button etc.
For Swift 4
Select your tableviewController Scene in storyboard
Drag and drop UIView Add label with your message (eg: No Data)
create outlet of UIView (say for eg yournoDataView) on your TableViewController.
and in viewDidLoad
self.tableView.backgroundView = yourNoDataView