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

    Joined:
    Mar 21, 2011
    #1
    Hello,

    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.

    ToDoListController.h:
    Code:
    #import <Cocoa/Cocoa.h>
    
    @interface ToDoListController : NSObject {
        IBOutlet NSTableView *tvToDoList;
        IBOutlet NSTextField *txtInput;
    	NSMutableArray *toDoList;
    }
    - (IBAction)cbAddEntry:(id)sender;
    @end
    
    ToDoListController.m:
    Code:
    
    #import "ToDoListController.h"
    
    @implementation ToDoListController
    
    -(id)init
    {
    	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
    								  row:(int)row
    {
    	NSString *value = [toDoList objectAtIndex:row];
    	
    	return value;
    }
    
    -(IBAction)cbAddEntry:(id)sender
    {
    	NSString *sUserInput = [txtInput stringValue];
    	
    	NSLog(@"sUserInput=%@", sUserInput);
    	
    	[toDoList addObject:sUserInput];
    	
    	NSLog(@"toDoList={%@}", toDoList);
    	
    	[txtInput setStringValue:@""];
    }
    
    @end
    
     
  2. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    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

    Joined:
    Mar 21, 2011
    #3
    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

    Sydde

    Joined:
    Aug 17, 2009
    #4
    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

    Joined:
    Aug 9, 2009
    #5
    Try naming things what they are, instead of abbreviating what they are:
    Code:
    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

    Joined:
    Feb 29, 2004
    #6
    you are leaking toDoList

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

    Sydde

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

    Joined:
    Mar 21, 2011
    #8
    Is

    Code:
    -(void)dealloc
    {
        [toDoList release];
        [super dealloc];
    }
    
    all I need?
     

Share This Page