Problems using NSMutableArray with NSTableView (Cocoa)

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

//
//  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 line I initialize the array with the display, but I can not delete it or add new lines.

+3
source share
3 answers

, , , / , . , [tableView reloadData] addToDo, removeToDo clearAllToDos.

[NSMutableArray arrayWithObject;...] , retain.

+3

. , toDoList = [[NSMutableArray arrayWithObject:@"Add a ToDo with the '+' button"] retain]; [toDoList removeObjectAtIndex:[tableView selectedRow]]; .
. , . . , [toDoList removeObjectAtIndex: .

+2

AppController.m arrayWithObject: autorelease, -. initWithObjects:

0

Source: https://habr.com/ru/post/1784489/


All Articles