Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

rafer11

macrumors newbie
Original poster
Mar 21, 2011
11
0
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
 
Last edited:
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.)
 
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.
 
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.)
 
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.
 
Is

Code:
-(void)dealloc
{
    [toDoList release];
    [super dealloc];
}

all I need?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.