views:

304

answers:

3

I have a UITableViewController, when there is no data to populate the UITableView, I want to add a button, which uses an image. So, rather than the user seeing a tableview with no records, they will see an image that says, "No records have been added, Tap to add one", then they click and we create a new one.

I assumed I would just hide the UITableView, then create the button, but I never see the button. Here I am using:

if ([[fetchedResultsController sections] count] == 0) {
        self.tableView.hidden = YES;
        // Create button w/ image
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        btn.frame = CGRectMake(0, 0, 100, 50);
        [btn setImage:[UIImage imageNamed:@"no-rides.png"] forState:UIControlStateNormal];
        [self.view addSubview:btn];
    }

Ideas on why I would never see the button? When I show this view, it seems to have a transparent background for a second, then changes white...

+1  A: 

In a UITableViewController, self.view could be self.tableView in which case hiding the table would also hide the button. Try using a custom UIViewController and creating either the table or the button as a subview of self.view instead.

Alternately, when there is no data, you can create a single custom cell containing your button and use that instead of normal cells.

drawnonward
+1  A: 

I am not sure if it works but you can try this:

1/ If your view controller is not a UITableViewController and it contains a UITableView [self.tableView removeFromSuperView]; then [self.view addSubView];

2/ If your view controller is a UITableViewController, you may need to consider to set the first row to contain the image and text. Then, you can handle the event: tableView:didSelectRowAtIndexPath: and if user click on the first cell, you trigger the method handle the button event

vodkhang
Ok, I tried no hiding my tableView, and I see that my image is getting create inside cell 0. Does this mean that my view controller is the tableviewcontroller? Not sure I want to keep it in that cell and customize it, because I don't want the button to be scrollable.
Nic Hubbard
Does your class inherit from UITableViewController or UIViewController? If from UITableViewController, so it is a UITableViewController
vodkhang
UITableViewController
Nic Hubbard
Oh, so it is a UITableViewController, so you can not hide it. The only way now it try to set the first cell to the text and the image. But because it is in specific the first cell, it will not be scrollable
vodkhang
A: 

I believe you can add the button to the table footer, as the table footer is shown even when there are no cells. (Note, this is different to a section footer.)

By default the tableFooterView property of the UITableView is nil. So just create your button, and then do:

self.tableView.tableFooterview = btn;
Emil