UITableView missing the table


#1

Once I set the editing property for the view controller and implemented the toggleEditingMode- I ran the app to see the header in the proper place and working, but the table of the rows of things to do became missing. So, to clarify, the only thing showing were the “done” and “new” buttons. Please help! Thank you so much.

[code]-(id)init
{
//call superclass’s designated initilaizer
self = [super initWithStyle:UITableViewStyleGrouped];
if (self) {
for (int i = 0; i < 5; i++) {
[[BNRItemStore sharedStore] createItem];
}
}
return self;
}

-(id)initWithStyle:(UITableViewStyle)style{
return [self init];
}

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    return [[[BNRItemStore sharedStore] allItems] count];
    }

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    //check for a reusable cell first, use that if it exists
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@“UITableViewCell”];

    //if there is no reusable cell of this type, create a new one
    if(!cell){
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@“UITableViewCell”];
    }

    //set the text on the cell with the description of the item that’s on the nth index of items.
    //where n = row this cell will appear on in the tableview
    BNRItem *p = [[[BNRItemStore sharedStore] allItems]objectAtIndex:[indexPath row]];
    [[cell textLabel] setText:[p description]];
    return cell;
    }

  • (UIView *)headerView
    {
    //if we haven’t loaded the headerView yet…
    if(!headerView){
    //load headerView.xib
    [[NSBundle mainBundle] loadNibNamed:@“HeaderView” owner:self options:nil];
    }
    return headerView;
    }

  • (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
    return [self headerView];
    }

  • (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
    //height of the header view should be determined from the height of the view in xib file
    return [[self headerView] bounds].size.height;
    }

  • (IBAction)toggleEditingMode:(id)sender
    {
    //if we are currently in editing mode : : :
    if ([self isEditing]) {
    //change the text of button to inform user of state
    [sender setTitle:@“Edit” forState:UIControlStateNormal];
    //turn off editing mode
    [self setEditing:NO animated:YES];
    } else {
    //change text of button to inform user of state
    [sender setTitle:@“Done” forState:UIControlStateNormal];
    //enter editing mode
    [self setEditing:YES animated:YES];
    }
    }
    [/code]


#2

You have the “Done” Button before clicking on “Edit”?
Do you set the header view to transparent?


#3

I have the same issue.
I did set the custom table header view to “transparent”. I also tried to resize the custom view.

I checked solutions provided with the book and it also doesn’t show the table - only the header.
I guess it is because some predefined settings that came with f* xcode 5. Great to update to a new version of software hoping to get a batter experience and instead, learn that what have worked before - doesn’t work anymore.


#4

I’ve solved my issue.

First - in the sample code provided with the book - when we run it - there are now table view created. If we push the “add” button - first row will appear. If you will look in the ItemViewController.m file method “init” - there is nothing inside the “if” statement, therefore when we run the app - no BNRItems are created.

Second - the reason why we aren’t getting our table views is most likely because of the wrong connections in the .xib file. In the book, authors decided to omit explanation on how to make connections with our custom created .xib file. I personally, had some hard time specifically with those xib files connections. What I did is, instead of connecting headerView instance with the view in the builder, I connected the “view” instance.

Here is the step by step process of setting your .xib file:

  1. Make sure that you’ve declared “headerView” instance variable in the ItemsViewController.h file. Also, methods “addNewItem” and “toggleEditingMode” should be declared in the header file. (this step was explained in the book).

  2. Create new .xib file, choosing an “Empty Template”. Name it “HeaderView” (explained in the book).

  3. Choose the .xib file. Chose the “File’s Owner” on the “Outline View” pane that is to the left of the builder. Now go to left “Utilities” pane and the click “identity Inspector” on top. Change “Class” field to “ItemsViewController”. What we did is we specified that this .xib file’s owner is “ItemsViewController”. In the book this concept presented as “ItemsViewController will drop itself in the “File’s Owner” hole”.

  4. From the “Object Library” that is in the bottom of the left pane, we choose “View” object and drag it to the builder. Then we choose two “button” objects and Drag them to the view. In the “Attributes Inspector” change the default title of the buttons to “Edit” and “New” respectively.
    You need to understand very important thing - this view is not our main view, it is just a header view for our table. Right now in your builder, the table header view looks like the main view because it has size of the 4 inch iPhone screen. We need to change the size of the table header view so it will look like the one in the book. To change the view size choose the “View” object in the right pane “Outline View”. Now go to the left pane and choose “Attributes Inspector”. Change the “size” field to “None”.
    Now, in your builder, three dots will appear on corners and sides of you table view. Click on the bottom ones and minimize the view so it will be big enough to host only two buttons.
    While in the “Attribute Inspector” of the “View” object we need to change one more thing. Find “Background” field and click on it. “Colors” window will show up. Drag the “Opacity” slider all the way to the left. That will make our view completely transparent.

5.Now the unexplained part of connections. On the right pane “Outline View” choose “File’s Owner”. “Control+Click” to see the popup menu of connections. Find “headerView” instance in “Outlets” and drag it down to the builder on our table view.
Now, locate “addNewItem” method in the “Received Actions” of the pop up menu. Drag it to the button that you’ve called “New”. A new menu will pop up - choose “Touch Up Inside”. Those are different actions that user can take when interacting with the UI elements. “Touch Up Inside” is the code name for simple “tap”.
Again “Control Click” on the “File’s Owner”. Locate the “toggleEditingMode” method in the “Received Actions” of the pop up menu. Drag it to the button that you’ve called “Edit”. Also choose “Touch Up Inside”.

Done.


#5

This makes it work. I made my background green to make sure I saw it: