Attaching a NSTableView to an Array

Discussion in 'Mac Programming' started by rafer11, Mar 21, 2011.

  1. rafer11, Mar 21, 2011
    Last edited: Mar 21, 2011

    rafer11 macrumors newbie

    Mar 21, 2011

    I'm currently going through the Aaron Hillegass book, and I am doing the challenge at the end of Chapter 6. The challenge is to create a program with 3 controls (NSTextField for input, NSTableView for display, and an NSButton to add what's in the NSTextField to the NSTableView. The program is supposed to be a 'To Do List').

    How do I attach/connect my array to the NSTableView? I am interested in doing this via Outlets (not through binding).

    Here is the code I have so far. Everything works except for connecting these two pieces.

    #import <Cocoa/Cocoa.h>
    @interface ToDoListController : NSObject {
        IBOutlet NSTableView *tvToDoList;
        IBOutlet NSTextField *txtInput;
    	NSMutableArray *toDoList;
    - (IBAction)cbAddEntry:(id)sender;
    #import "ToDoListController.h"
    @implementation ToDoListController
    	if (![super init])
    		return nil;
    	toDoList = [[NSMutableArray alloc] init];
    	return self;
    -(int)numberOfRowsInTableView:(NSTableView *)tv
    	return [toDoList count];
    -(id)tableView:(NSTableView *)tv
    			objectValueForTableColumn:(NSTableColumn *)tableColumn
    	NSString *value = [toDoList objectAtIndex:row];
    	return value;
    	NSString *sUserInput = [txtInput stringValue];
    	NSLog(@"sUserInput=%@", sUserInput);
    	[toDoList addObject:sUserInput];
    	NSLog(@"toDoList={%@}", toDoList);
    	[txtInput setStringValue:@""];
  2. jiminaus macrumors 65816


    Dec 16, 2010
    Adding an elements to toDoList won't cause tvToDoList to be updated. You'll need to send a reloadData message to tvToDoList after you update toDoList.

    (BTW I hope Hillgrass didn't teach your reverse Hungarian notation. That kind of visual noise should be left with Windows and Visual Basic.)
  3. rafer11 thread starter macrumors newbie

    Mar 21, 2011
    Got it.

    Awesome. Got it, thanks.

    Your comment has got me searching google for the pitfalls and cons of hungarian notation. I'm just used to it from my job, so I had it in my code.

    What do you consider the downfalls of hungarian notation? I've come to like it for readability and debugging.
  4. Sydde macrumors 68020


    Aug 17, 2009
    Is "sUserInput" somehow clearer than "userInput"? In the context of a method or object, a descriptive name should be more than sufficient to figure out what a thing is supposed to be. And then, you are not being consistent: what does "to" represent in toDoList? Should it not be "maToDoList", or at least "aToDoList"? (There are scores of NextStep classes in Cocoa, the prefixes could get awkward.)
  5. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    Try naming things what they are, instead of abbreviating what they are:
    IBOutlet NSTableView *toDoTableView;
    IBOutlet NSTextField *toDoTextToAdd;
    NSMutableArray *toDoArray;
    I don't see how tiny abbreviations like tv enhance readability, when the major part of the name (ToDoList) is identical between variables. I would rather see the major part of the name different.

    Name variables like characters in a novel. You may know four people in real life whose name is Joe. But if you give four different characters in your novel the name Joe, readers will think there's a plot reason, or they'll just think you're a poor writer.
  6. mshepherd macrumors regular

    Feb 29, 2004
    you are leaking toDoList

    I would set those up as properties also.
  7. Sydde macrumors 68020


    Aug 17, 2009
    Unless GC is on?
  8. rafer11 thread starter macrumors newbie

    Mar 21, 2011

        [toDoList release];
        [super dealloc];
    all I need?

Share This Page