Cannot select table row after modal add item


#1

I’ve now gone through the chapter twice and the same thing is happening. After making it so adding an item brings up the modal view, the table rows are not selectable. I’ve implemented the ‘Save’ and ‘Cancel’ buttons in the modal so I am able to add as many items as I want. When I get back to the table view and try to select an item to see the detail view I am not able to. Also, am unable to edit the items in the table (delete / rearrange order). Its as if there is an invisible view that is blocking any input. I’m completely stuck. :question:

UPDATE:
I went back through my code and compared it to the BNR sample code. The only difference is that my code creates the extra table row that says “No more items!”. When I comment this out I am once again able to select a row and bring up the detail view. Why this happens doesn’t make a whole lot of sense to me though. Thoughts anyone?

Here’s where I create the extra row in itemsViewController.m

[code]- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[[BNRItemStore sharedStore] allItems] count] + 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

// Check for a resuable cell first, use that if it exists
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];

// If there is no resusable 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 is at the nth index of items, where n = row this cell
// will appear in on the tableview

if ([indexPath row] < [[[BNRItemStore sharedStore] allItems] count]) {
    BNRItem *p = [[[BNRItemStore sharedStore] allItems] objectAtIndex:[indexPath row]];
    [[cell textLabel] setText:[p description]];
} else {
    [[cell textLabel] setText:@"No more items!"];
    [cell setUserInteractionEnabled:NO];
}

return cell;

}

[/code]

Is there something wrong with this that would affect selecting a table row?


#2

You need to explicitly enable interaction for the other rows as shown below. This has something to do with reusing cells.

Here’s what I think is happening. When you start the app, the first cell added is for the “No more items” row and has interaction explicitly turned off. When you start adding a second row, the detail view is displayed and the table goes away. This puts that first cell in the pool to be reused. Prior to displaying the table again, the first cell is dequeued and used for the new row. Then a new cell is created for the “No more items” row and interaction is explicitly turned off for that cell. Basically, every cell that goes in the pool was originally used for “No more items” at some point and, thus, will have interaction turned off. That’s why you need to turn interaction back on for the other cells.

    if ([indexPath row] < [[[BNRItemStore sharedStore] allItems] count]) {
        BNRItem *p = [[[BNRItemStore sharedStore] allItems] objectAtIndex:[indexPath row]];
        [[cell textLabel] setText:[p description]];
        [cell setUserInteractionEnabled:YES];
    } else {
        [[cell textLabel] setText:@"No more items!"];
        [cell setUserInteractionEnabled:NO];
    }

#3

After reading your response a couple times I think I get it. Explicitly enabling the interaction on the other cells works perfectly. Thanks!