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?
This is the best and simple solution.
UITableView's backgroundView property is your friend.
In
viewDidLoad
or anywhere that youreloadData
you should determine if there your table is empty or not and update the UITableView's backgroundView property with a UIView containing a UILabel or just set it to nil. That's it.It is of course possible to make UITableView's data source do double duty and return a special "list is empty" cell, it strikes me as a kludge. Suddenly
numberOfRowsInSection:(NSInteger)section
has to compute the number of rows of other sections it wasn't asked about to make sure they are empty too. You also need to make a special cell that has the empty message. Also don't forget that you need to probably change the height of your cell to accommodate the empty message. This is all doable but it seems like band-aid on top of band-aid.First, the problems with other popular approaches.
BackgroundView
Background view doesn't center nicely if you were to use the simple case of setting it to be a UILabel.
Cells, headers, or footers to display the message
This interferes with your functional code and introduces weird edge cases. If you want to perfectly center your message, that adds another level of complexity.
Rolling your own table view controller
You lose built-in functionality, such as refreshControl, and re-invent the wheel. Stick to UITableViewController for the best maintainable results.
Adding UITableViewController as a child view controller
I have a feeling you'll end up with contentInset issues in iOS 7+ - plus why complicate things?
My solution
The best solution I've come up with (and, granted, this isn't ideal) is to make a special view that can sit on top of a scroll view and act accordingly. This obviously gets complicated in iOS 7 with contentInset madness, but it's doable.
Things you have to watch out for:
Once you have this figured out once in one UIView subclass, you can use it for everything - loading spinners, disabling views, showing error messages, etc.
Probably not the greatest solution, but I did this by just putting a label at the bottom of my table and if the rows = 0 then I assign it some text. Pretty easy, and achieves what you are trying to do with a few lines of code.
I have two sections in my table (jobs and schools)
Using the backgroundView is fine, but it does not scroll nicely like in Mail.app.
I did something similar to what xtravar did.
I added a view outside the view hierarchy of the
tableViewController
.Then i used the following code in
tableView:numberOfRowsInSection:
:Basically I added the
emptyStateView
as a subview of thetableView
object. As the separators would overlap the view, I set their color toclearColor
. To get back to the default separator color, you can just set it tonil
.