Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having problems using NSMutableArray with NSTableView (Cocoa)

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.

like image 932
rpowell Avatar asked Feb 26 '26 10:02

rpowell


2 Answers

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.

like image 52
mrbaboo Avatar answered Mar 01 '26 00:03

mrbaboo


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.

like image 38
Neilvert Noval Avatar answered Feb 28 '26 23:02

Neilvert Noval



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!