Answer for part B, compiles in XCode10


#1

Yes! The last person on the planet to be learning Objective-C in XCode… here is my answer to the B part of the challenge. RootViewController here is both the dataSource AND delegate (meaning bindings have been set as such).

Critiques please… I know I didn’t do lazy instantiation of the _thingsToDo array… I could have. Also, this only works with Cell based tables, with View based it did not work. Did not use storyboards… long story.

//  RootViewController.h

#import <Cocoa/Cocoa.h>

NS_ASSUME_NONNULL_BEGIN

@interface RootViewController : NSViewController <NSTableViewDataSource, NSTableViewDelegate>

@property (strong, nonatomic) NSMutableArray *thingsToDo;
@property(readonly) NSInteger selectedRow;

@end

NS_ASSUME_NONNULL_END



//  RootViewController.m

#import "RootViewController.h"

@interface RootViewController ()
@property (weak) IBOutlet NSWindow *window;

@property (weak) IBOutlet NSTextField *things;
@property (weak) IBOutlet NSTableView *table;

@end

@implementation RootViewController

-(NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
    return [_thingsToDo count];
}

-(id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    NSString *t = [_thingsToDo objectAtIndex:row];
    //NSLog(@"t value: %@", t);
    return t;
}

- (IBAction)addButton:(NSButton *)sender
{
    NSString *ttd = [self.things stringValue];
    //NSLog (@"string = %@", ttd);
    [_thingsToDo addObject:ttd];
    //NSLog (@"things in array: %lu", [_thingsToDo count]);
    [self.table reloadData];
}

- (IBAction)removeButton:(NSButton *)sender
{
    NSInteger i = _selectedRow;
    [_thingsToDo removeObjectAtIndex:i];
    [self.table reloadData];
}

- (void)tableViewSelectionDidChange:(NSNotification *)notification
{
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _thingsToDo = [[NSMutableArray alloc] init];
    [_table setDelegate:self];
    
}

@end