简体   繁体   中英

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.

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 .

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM