0

I want to display an empty message to the user when the UITableView is empty. There are a few attempts out there including using

  • backgroundView: My UILabel stays centered but if you scroll the table view, the message position stays fixed. This looks kind of weird with my current layout.
  • addSubView: This was my currently used solution. Because I added another view auto layout destroyed my approach. Here the message goes with the scrolling as needed but it's not that useable because of side effects.
  • header/footer view: How do you center here correctly and what about the height?
  • a special cell: How do you have a correct height here? What about centering?

I want to use auto layout in my case if possible. Here are my requirements:

  • Message (could be also an image with a text) should be centered in the table view.
  • If the table view is scrolled, the message should have a fixed position on the table view. It should scroll with the label like it would be label on the scroll view, but the height of the scroll view correlates to the screen height.
  • Pull to refresh should be possible.

One example can be seen in the app "app store". If you are in flight mode and you navigate to the tab "updates" you can see this behavior. On iPhone it seems to be a separate cell. Don't know what it is on iPad. Perhaps the same. But how can this be achieved?

1 Answer 1

1

There are two approaches you can take here:

Approach 1 - Show/Hide (Cleaner)

You can conditionally hide or show the tableview based on whether it has any content. If it has no content, hide the tableview and show whatever placeholder view you want to show. Whenever your data refreshes, simply call the method again to determine which view to show.

Approach 2 - Special Section

You can create an entirely new section for you "No content" table view message, and in tableView:numberOfSectionsInTableview return 1 if there is no content, conditionally showing your special cell just for that case.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your reply. Do you have a code sample for approach No. 1? I tried it together with Auto Layout, but this doesn't work well if one add a subview to an UITableView ... Auto Layout still required after executing -layoutSubviews. UITableView's implementation of -layoutSubviews needs to call super.
Another thing: Approach 1 will not work for me because the user should be able to refresh the list. After a refresh there can be data available. Completetly hiding the table view will also remove my pull-to-refresh function. Some thoughts about approach 2: Can I instruct auto layout to use a cell height which uses the complete screen/table view?
@testing You will not be able to use autolayout to set the cell height, but you can override -(CGFloat)tableView:heightForRowAtIndexPath and conditionally set it to CGRectGetHeight(tableView.bounds) to get a cell that takes up the whole screen.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.