Edit/delete with tableview sections from Chap. 9


#1

I tried combining my challenge code from the last chapter with the editing mode in this chapter. In the challenge, I used two arrays built in ItemsViewController’s init method: aPriceyItems and aCheapItems.

I am doing something wrong with the delete code; it doesn’t go away when I press the button. How does one get the correct index for the individual array? Also, I get a warning, “Unused variable p” on both “BNRItem *p” lines in the IF statement.

Is it possible to have only one set of buttons, but keep the second header?

Thank you.

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // if the table view is asking to commit a delete command --
    if(editingStyle == UITableViewCellEditingStyleDelete) {
        
        // delete the stored object
       
        BNRItem(*p);
        
        if(indexPath.section == 0) {
            BNRItem *p = [aPriceyItems objectAtIndex:[indexPath row]];
        } else {
            BNRItem *p = [aCheapItems objectAtIndex:[indexPath row]];
        }
        
        BNRItemStore *ps = [BNRItemStore sharedStore];
        [ps removeItem:p];
        
        if(indexPath.section == 0) {
            [aPriceyItems removeObjectAtIndex:[indexPath row]];
        } else {
            [aCheapItems removeObjectAtIndex:[indexPath row]];
        }       
        
    }
    
}

#2

I don’t have the 3rd Edition of the book, but does this code actually compile without any errors let alone the unused-variable warnings?

I can see two problems.

P1:

...
BNRItem (*p);
...

What does the above statement, which looks like a function call, do? Where is p declared? Is it a global variable?
P2:

...
BNRItemStore *ps = [BNRItemStore sharedStore];
[ps removeItem:p];
...

Again where is p declared? Is it a global variable?

As for the warnings, you are having them because you are creating variables and never using them. A variable can only be used in a scope it is is visible in. The statement if (…) {…} else {…} creates two scopes; any variable created in those scopes can’t be seen from outside.

Maybe, you meant to do this:

...
        BNRItem * p;
        if(indexPath.section == 0) {
            p = [aPriceyItems objectAtIndex:[indexPath row]];
        } else {
            p = [aCheapItems objectAtIndex:[indexPath row]];
        }
        
        BNRItemStore *ps = [BNRItemStore sharedStore];
        [ps removeItem:p];

...

#3

Hi Ibex,

Your suggestions removed the warning messages; thank you for identifying the syntax problem (I’m a complete neophyte, with only the Objective-C book under my belt).

Yes, the code compiles without any errors (the green check mark for “build succeeded”). However, it still does not remove the item when I click on the delete button in the simulator.

There is a statement in BNRItem.m which I tried commenting out, fearing it superceded the actual item deletion, but it didn’t make any difference,

- (void) dealloc // but since this supercedes the higher-up method, shouldn't we deallocate as well as send a message? { NSLog(@"Destroyed: %@", self); }

Is there something like a strong reference which is preventing the listed item from being deleted?

Thank you,
Don