Am I over-complicating table datasources?

Discussion in 'Mac Programming' started by robvas, Feb 26, 2012.

  1. macrumors 68000

    Joined:
    Mar 29, 2009
    Location:
    USA
    #1
    I wanted to make a table with the datasource as a seperate object from my AppDelegate. It seems like I am creating functions to get/set the data that should already be there? I'm probably just doing it wrong. The program works, it just seems repetitive. Mainly the datasource model - am I missing a way to use the 'tableview' functions to get/set the data rows?

    AppDelgate.m
    Code:
    //
    //  AppDelegate.m
    //  Tables
    
    #import "AppDelegate.h"
    
    @implementation AppDelegate
    
    //@synthesize myTable = _myTable;
    @synthesize window = _window;
    //@synthesize input;
    
    - (void)dealloc
    {
        [myTable release];
        [myData release];
        [super dealloc];
    }
    
    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
    {
        // Insert code here to initialize your application
        [myTable setBackgroundColor:[NSColor grayColor]];
        
        myData = [[MyDataSource alloc] init];
        [myTable setDataSource:myData];
        [myTable setDelegate:self];
        selectedRow = -1;
    }
    
    // methods
    
    - (IBAction)addItem:(id)sender {
        NSLog(@"Adding %@", [input stringValue]);
        [myData addItem:[input stringValue]];
        [myTable reloadData];
    }
    
    - (IBAction)removeItem:(id)sender {
        if (selectedRow > -1) {
            NSLog(@"Removing %@", [myData getItemAtColumn:0 row:selectedRow]);
            [myData removeItemAt:(int)selectedRow];
            [myTable reloadData];
        }
    }
    
    
    - (void)tableViewSelectionDidChange:(NSNotification *)notification {
        selectedRow = [myTable selectedRow];
        NSLog(@"Selected row is %ld", selectedRow);
    }
    
    @end
    
    MyDataSource.m
    Code:
    //
    //  MyDataSource.m
    //  Tables
    
    
    #import "MyDataSource.h"
    
    @implementation MyDataSource
    
    - (id)init {
        items = [[NSMutableArray alloc] init];
        [items addObject:[NSString stringWithString:@"Very first object"]];
        [items addObject:[NSString stringWithString:@"Another object"]];
        [items addObject:[NSString stringWithString:@"Enough objects for now"]];
        
        return self;
    }
    
    - (void)addItem:(id)item {
        NSLog(@"addItem: %@", item);
        [items addObject:item];
    
    }
    
    - (void)removeItemAt:(int)index {
        NSLog(@"removeItem: %d", index);
        [items removeObjectAtIndex:index];
    }
    
    - (id)getItemAtColumn:(NSInteger)col
                        row:(NSInteger)row {
        return [items objectAtIndex:row];
    }
    
    - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
        return [items count];
    }
    
    - (id)tableView:(NSTableView *)tableView 
    objectValueForTableColumn:(NSTableColumn *)tableColumn
                row:(int)row {
        return [items objectAtIndex:row];
    }
    
    - (void)tableView:(NSTableView *)tableView
       setObjectValue:(id)object
       forTableColumn:(NSTableColumn *)tableColumn
                  row:(int)row {
        [items addObject:object];
    }
    
    - (void)dealloc {
        [items release];
        [super dealloc];
    }
    
    @end
    
    My app looks like this:

    [​IMG]

    Basically you just add/delete rows. If there's anything else I'm doing wrong that I'm not realizing please point it out to me, the more tips/advice the better.

    See the attached project for more details.
     

    Attached Files:

  2. macrumors 68000

    Sydde

    Joined:
    Aug 17, 2009
    #2
    This is not the correct way to implement this method. Which is kind of irrelevant, since it will not be getting called in the first place. But just for fun, change you tableview to editable in IB and see what happens when you double click and type into one of the cells. It will be a good lesson, once you figure out what is wrong.
     

Share This Page