简体   繁体   中英

How do I display a no-data splash UIView in a UITableViewController? Add and remove view from tableView?

How do I add or remove a view from a TableView?

I want it to come up with "No Connections" whenever there is nothing in the tableView, but then when there is data, I just want a normal tableView.

I've checked lots of questions, all of which had strange ways of removing views from cells and doing everything in a cell. However, I also found that you can, in Xcode, drag a UIView into a tableView, and it will scroll with it.

When you do that, and you want to get rid of the view, you simply set the hidden property of that view to be true.

The problem here is that then there is white space where the view once was. I want it to completely go away and not be there, but adding/removing subviews didn't work for me. I tried lots of other solutions as well.

I found that you can simply put that View into the bucket in Xcode, but not in the view controller. (See pic)

不在视图控制器中

Then, I found that if I made an outlet to that UIView , I could still use it. Note that I had to design the view and make it the proper size, etc either programatically, or in IB before I moved it to this position, since I cannot see it anymore in IB.

Then, in the TableViewController class, I used ViewWillAppear to check the count of the array that backs up the TableView. If it was zero, I set the view (using the Outlet) to be hidden or not.

Next, I used this code:

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (noConnectionView.hidden)
    {
        return 0;
    } else {
        return 480;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (noConnectionView.hidden)
    {
        return nil;
    } else {
        return noConnectionView;
    }
}

This makes it fill my popover, and since the number of cells for section method returns the same count that I already checked (which is zero, if the table is empty) that is the only thing displayed (no empty cells).

When there are things to display, ViewWillAppear is called each time (and whenever things are deleted, I call view will appear again) and then the tableView gets rid of the header and it behaves like a normal tableView.

This is great because you can use it with a UITableViewController or in sequence with a navigation controller.

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