ToDo challenge


#1

AppDelegate.h:

#import <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject <NSApplicationDelegate, NSTableViewDelegate, NSTableViewDataSource>
{
    NSMutableArray *toDoArray;
}
@property (weak) IBOutlet NSTableView *toDoTable;
@property (assign) IBOutlet NSWindow *window;
@property (weak) IBOutlet NSTextField *toDoField;
- (IBAction)addItemToDoToTable:(id)sender;
- (IBAction)deleteSelectedRow:(id)sender;
@end

AppDelegate.m

#import "AppDelegate.h"

@implementation AppDelegate

- (id)init
{
    self = [super init];
    if (self) {
        toDoArray = [[NSMutableArray alloc] init];
    }
    return self;
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
}

- (IBAction)addItemToDoToTable:(id)sender {
    NSString *toDo = [[self toDoField] stringValue];
    if (toDo.length == 0)
    {
        NSAlert *av = [NSAlert alertWithMessageText:@"Oops" defaultButton:@"OK" alternateButton:nil otherButton:nil informativeTextWithFormat:@"Please enter something in the text field" ];
        [av runModal];
    }
    else {
        [[self toDoField] setStringValue:@""];
        [toDoArray addObject: toDo];
        [[self toDoTable] reloadData];
    }
}

- (IBAction)deleteSelectedRow:(id)sender {
    NSInteger selectedRow = [[self toDoTable] selectedRow];
    if (selectedRow < 0) {
        NSAlert *av = [NSAlert alertWithMessageText:@"Error" defaultButton:@"OK" alternateButton:nil otherButton:nil informativeTextWithFormat:@"Please select an item you wish to delete"];
        [av runModal];
        return;
    }
    
    //no error proceed
    [toDoArray removeObjectAtIndex: selectedRow];
    [[self toDoTable] reloadData];
}

#pragma mark -
#pragma mark Delegation Methods

- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView {
    return [toDoArray count];
}

- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex {
    
    return [toDoArray objectAtIndex: rowIndex];
}

@end