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?
One way of doing it would be modifying your data source to return
1
when the number of rows is zero, and to produce a special-purpose cell (perhaps with a different cell identifier) in thetableView:cellForRowAtIndexPath:
method.This may get somewhat complicated if you have multiple table view controllers that you need to maintain, because someone will eventually forget to insert a zero check. A better approach is to create a separate implementation of a
UITableViewDataSource
implementation that always returns a single row with a configurable message (let's call itEmptyTableViewDataSource
). When the data that is managed by your table view controller changes, the code that manages the change would check if the data is empty. If it is not empty, set your table view controller with its regular data source; otherwise, set it with an instance of theEmptyTableViewDataSource
that has been configured with the appropriate message.I have been using the titleForFooterInSection message for this. I don't know if this is suboptimal or not, but it works.
Swift version but better and simpler form . **3.0
I hope it server your purpose......
In your UITableViewController .
Helper Class with function :
I can only recommend to drag&drop a UITextView inside the TableView after the cells. Make a connection to the ViewController and hide/display it when appropriate (e.g. whenever the table reloads).
Same as Jhonston's answer, but I preferred it as an extension:
Usage:
Based on the answers here, here is a quick class I made that you can use on in your
UITableViewController
.In your
UITableViewController
you can call this innumberOfSectionsInTableView(tableView: UITableView) -> Int
With a little help from http://www.appcoda.com/pull-to-refresh-uitableview-empty/