small medium large xlarge

Generic-user-small
22 Oct 2010, 13:08
Simon Wragg (4 posts)

Had a go - not sure why this wont work;

(IBAction) removeItemFromShoppingList:(id)sender

{ int clickedRow =[shoppingListTableView clickedRow]; [removeObjectAtIndex:clickedRow]; [shoppingListTableView reloadData];

}

Generic-user-small
22 Oct 2010, 13:11
Simon Wragg (4 posts)

(IBAction) removeItemFromShoppingList:(id)sender

{ int clickedRow =[shoppingListTableView clickedRow]; [removeObjectAtIndex:clickedRow]; [shoppingListTableView reloadData];

} Sorry 1st post missed some code!

Generic-user-small
22 Oct 2010, 13:12
Simon Wragg (4 posts)

2nd post also missed some! (IBAction) removeItemFromShoppingList:(id)sender {int clickedRow =[shoppingListTableView clickedRow]; [removeObjectAtIndex:clickedRow]; [shoppingListTableView reloadData];}

Generic-user-small
27 Oct 2010, 07:52
Tim Isted (105 posts)

Sorry for the delay. There’s an error in your @removeObjectAtIndex:@ line - you don’t specify an object as a receiver of the @removeObjectAtIndex:clickedRow@ message, only the message.

If you’ve lost lines of code because of the way the forum posting works, try posting code surrounded by ~~~

~~~

or <code>

Generic-user-small
15 Aug 2011, 09:25
Stephen Bolton (4 posts)

I like the challenge you threw down in this chapter Tim!

I am beyond frustrated though because my code doesn’t work

- (IBAction)removeItemFromList:(id)sender
    {
        long int indexOfSelectedItem = [shoppingListTableView clickedRow];
        
        [shoppingListArray removeObjectAtIndex:indexOfSelectedItem];
        
        [shoppingListTableView reloadData];
    }

I always get a Breakpoint to pop up and cannot resolve it.

Generic-user-small
15 Aug 2011, 09:38
Tim Isted (105 posts)

Hi Stephen,

Try changing @clickedRow@ to @selectedRow@, and you should find that it works.

The @selectedRow@ method will return the index of the row that’s currently selected (i.e. highlighted), or @-1@ if nothing is selected.

The @clickedRow@ method is used if you’re writing an action method that’s triggered by the table view itself, such as when a user double-clicks on a cell. Because the action method above is triggered by a separate button in the interface, the @clickedRow@ property will indicate @-1@ (i.e. no row was clicked).

I don’t have a copy of the book in front of me at the moment; do I suggest using the @clickedRow@ method in the text? You’ve got me worried I’ve sent you on a wild goose chase!

Also, I’m guessing that you’re using @long int@ above because of a compiler complaint about using a plain @int@? The best return type to use for the @clickedRow@ and @selectedRow@ methods is actually @NSInteger@, which is a scalar variable type provided by Apple.

I’d rather like to make some retroactive adjustments to the book to use the Apple-provided types rather than standard ints, etc. @NSInteger@ matches the return type specified in the documentation, and means the same thing as @long@ (when compiling for 64-bit, which means the majority of Intel-based Macs except for a few early ones like the original Mac Mini) but you don’t need to worry about exactly what it means. Just treat it like some kind of integer, in the knowledge that it’s at least the same kind of integer as Apple uses when returning an index.

Generic-user-small
15 Aug 2011, 10:21
Stephen Bolton (4 posts)

Well its funny I was using selectedRow previously but I saw the other poster in this topic using it so I tried it. I was using long for the reason you said the compiler didn’t like the conversion precision loss.

The code is still not working for me. When I check the debugger console it says that index is out of bounds and is returning an index number that is huge. Here is my code again.

- (IBAction)removeItemFromList:(id)sender
{
        long int indexOfSelectedItem = [shoppingListTableView selectedRow];
        
        NSLog(@"The current Selected Index is %li", indexOfSelectedItem);
    
    [shoppingListArray removeObjectAtIndex:indexOfSelectedItem];
    
    [shoppingListTableView reloadData];

Here is what the debugger is saying Shopping List[4849:407] *** -[__NSArrayM removeObjectAtIndex:]: index 18446744073709551615 beyond bounds [0 .. 2]

Generic-user-small
17 Aug 2011, 10:41
Tim Isted (105 posts)

Sorry for the delay, for some reason I didn’t see your reply in my feeds.

The index that you’re seeing in the log, @18446744073709551615@, is actually @(2^64 - 1)@. This is the two’s complement of the number @(1)@, which is the way a signed variable stores a negative number. In this case, the number being stored is -1.

There are two things going on here.

Firstly, are you definitely selecting a row in the table view before clicking the remove button? NSTableView’s selectedRow method will return @-1@ if no row is selected, and it sounds like this it what’s happening here.

The reason you’re seeing the error in the console is that NSArray’s removeObjectAtIndex: method expects to be given an NSUInteger, i.e., an unsigned integer value. When you provide your @indexOfSelectedItem@ as that index, which interpreted as signed is the value @-1@, the method interprets it as if it were unsigned, which is the @2^64 - 1@. The array only has three items in it, so @2^64 - 1@ is somewhat out of bounds.

Assuming that you select a row in the table view before clicking the button, this should be working. What’s missing is a check that the value isn’t less than zero, which should return early from the method before asking the array to remove anything. You’ll see how to do exactly this in Chapter 9, section 9.6 “Adding Conditional Statements to the Shopping List Application”.

You must be logged in to comment