I am having trouble trying to use NSMutableArray with an NSTableView controller in my simple ToDo list application. Adding items does not work, and trying to remove items gives me an error -[NSCFArray removeObjectAtIndex:]: index (0) beyond bounds (0). It seems that I can not find the cause of my problem, since trying to add an item does not return an error, and an error while deleting does not lead to an obvious solution. My code is as follows:
Appcontroller.h
#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
#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 line I initialize the array with the display, but I can not delete it or add new lines.
source
share