3

I'm having a problem trying to use an NSMutableArray with a NSTableView controller in my simple ToDo list application. Adding items does not work and trying to delete items gives me the error -[NSCFArray removeObjectAtIndex:]: index (0) beyond bounds (0). I can't seem to find the cause of my problem as trying to add an item returns no error and the error in removing one leads to no obvious solution. My code is as follow:

AppController.h

//
//  AppController.h
//  ToDo
//
//  Created by Rhys Powell on 10/01/11.
//

#import <Cocoa/Cocoa.h>
#import <BWToolkitFramework/BWToolkitFramework.h>

@interface AppController : NSObject {
    IBOutlet BWAnchoredButton *addButton;
    IBOutlet BWAnchoredButton *removeButton;
    IBOutlet NSMenuItem *clearAll;
    IBOutlet NSTableView *tableView;
    NSMutableArray *toDoList;
}
- (IBAction)addToDo:(id)sender;
- (IBAction)removeToDo:(id)sender;
- (IBAction)clearAllToDos:(id)sender;
@end

AppController.m

//
//  AppController.m
//  ToDo
//
//  Created by Rhys Powell on 10/01/11.
//

#import "AppController.h"


@implementation AppController

- (id)init
{
    [super init];

    NSLog(@"Initialising...");

    toDoList = [[NSMutableArray arrayWithObject:@"Add a ToDo with the '+' button"] retain];

    return self;
}

- (IBAction)addToDo:(id)sender
{
    NSLog(@"Added a ToDo");
    [toDoList addObject:@"Test"];
}

- (IBAction)removeToDo:(id)sender
{
    [toDoList removeObjectAtIndex:[tableView selectedRow]];
}

- (IBAction)clearAllToDos:(id)sender
{
    [toDoList removeAllObjects];
}

- (int)numberOfRowsInTableView:(NSTableView *)tv
{
    return [toDoList count];
}

- (id)tableView:(NSTableView *)tv objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row
{
    return [toDoList objectAtIndex:row];
}

@end

I should also note that the initial string I initialise the array with displays, but I cannot delete it or add new strings.

1
  • Show the calling code. Do you update the table view parallel to that? Commented Jan 10, 2011 at 10:28

3 Answers 3

3

Not sure of the underlying problem, but the first thing I see is you're deleting/adding the rows from the underlying array but not telling the table about this. Make sure you call [tableView reloadData] from within addToDo, removeToDo and clearAllToDos.

The [NSMutableArray arrayWithObject;...] call is fine as you also subsequently call retain.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, I'd only worked with static tables prior to this point so I didn't know I had to do this.
2

I have checked you code. And it seems that the toDoList = [[NSMutableArray arrayWithObject:@"Add a ToDo with the '+' button"] retain]; and the [toDoList removeObjectAtIndex:[tableView selectedRow]]; are doing well.
My perception on your problem is with regards to the table view. Make sure your table view always contains a selection. You can set that up in the Interface Builder or programmatically. This is to make sure that [toDoList removeObjectAtIndex: will always recieve a value from table view.

1 Comment

Yes, if there is no selection, -selectedRow returns -1.
0

In AppController.m you are creating the array with method arrayWithObject: this will create an autorelease array which will be released after something. so instead use initWithObjects: method

2 Comments

Xcode is telling me that there's no such method as initWithObject:.
NSArray *temp = [[NSArray alloc] initWithObjects:object, nil]; i am very sorry that i forgot to write 's' after Object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.