简体   繁体   中英

Putting a UIView in front of UITableView in a UITableViewController

I'm trying to put a UIView in front of the UITableView in my UITableViewController. The UIView is initially set to hidden. I'm making it so that if there's no items in the datasource...then the UIView is shown and says "sorry no items"...and this works when there are no items. However, the UITableViewController shows 6 items at a time, and whenever there are less than 6 items (but more than 0), it lets the user scroll down, and if they scroll down enough, my UIView is shown. How can I prevent this from happening? I think my code is only holding true per page not for the whole view controller, but not sure how to fix it. The weird thing is that Pagination is NOT enabled.

Whenever I've need to do something like this, I've taken to 1 of 2 options:

  1. Use a regular UIViewController with a UITableView added to it. That way, you can add the UIView (or maybe UILabel ) to the UIViewController 's view . Then, when you have no items to display, you can set the alpha of the UITableView to zero, and the label can be seen.
  2. Create a prototype UITableViewCell that displays the "No Items" message you want the user to see.

One of my apps does exactly this successfully. The UIView needs to be a peer of the UITableView (ie inside the same parent view) and in front of it (after it in the list of the parent's subviews).

Your storyboard should look like this:

Scene
  ViewController
    Top Layout Guide
    Bottom Layout Guide
    View
      Table View
      View  // the view which covers the table view.

Use constraints to make the table a fixed size relative to its superview, and to make the cover view the same size as the table and aligned with it, so it always covers it when visible.

It sounds like you've accidentally put the cover view inside the table or behind it or something.

Found a solution specifically if you are using a UITableView Controller. If your data set is empty, use this code: [self.tableView setSeparatorInset:UIEdgeInsetsZero]; . This removes the appearance of empty rows and makes your UITableView look like an empty UIView. Then, simply customize your view to make it how you want it. For my app, I added a label as a subview to the UITableView (if the data set was empty) giving my "no items" message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM