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

MorphingDragon

macrumors 603
Original poster
So the challenge is to make a To Do List with a TextField, TableView and a MutableArray.

I can add items to the array but I can't figure out how to update the TableView with the array contents. I'm pretty sure I've made the right connections in IB.

This is my code so far.

Code:
//
//  ToDoList.h
//  ToDoList
//
//  Created by Robert Litchfield on 7/05/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import <Cocoa/Cocoa.h>


@interface ToDoList : NSObject {
	NSMutableArray *toDoList;
	IBOutlet NSTableView *tableView;
	IBOutlet NSTextField *textField;
}

-(IBAction)addEntry:(id)sender;
-(id)init;
-(id)tableView:(NSTableView *)tv objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row;
-(int)numberOfRowsInTableView:(NSTableView *)tv;

@end
Code:
//
//  ToDoList.m
//  ToDoList
//
//  Created by Robert Litchfield on 7/05/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "ToDoList.h"


@implementation ToDoList

-(id)init {
	[super init];
	
	toDoList = [[NSMutableArray alloc] init];
	NSLog(@"Init Succesful");
	return self;
}

-(IBAction)addEntry:(id)sender {
	[toDoList addObject:[textField stringValue]];
	NSLog(@"Entry Added");
	
	NSLog(@"Array now:");
	for (NSString *ts in toDoList)
		NSLog(@"%@", ts);
	
}

-(int)numberOfRowsInTableView:(NSTableView *)tv {
	return [toDoList count];
}

-(id)tableView:(NSTableView *)tv objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row {
	NSString *c = [toDoList objectAtIndex:row];
	return c;
}
@end
 
The table normally does not watch the datasource, you have to advise it when a change has occurred. Look up the documentation to find the appropriate method for that (I am not telling you the method outright, because you probably should get in the habit of having your documentation handy).
 
The table normally does not watch the datasource, you have to advise it when a change has occurred. Look up the documentation to find the appropriate method for that (I am not telling you the method outright, because you probably should get in the habit of having your documentation handy).

Man, can't believe I overlooked something as simple as reloadData.
 
Would it happen to be a delegate method?

No. Conceptually that doesn't even make sense: the delegate methods are there for the table view to call to influence it's behaviour once it wants to do something. You are trying to tell it to do something so it can't be calling a delegate method: you are calling a method on it.

If you open the documentation for NSTableView the method is obvious. Try scrolling to tasks and looking at the method names.
 
No. Conceptually that doesn't even make sense:

Well Aaron didn't make it that clear in the first place. 😕

Cocoa is my first shot at a framework that primarily uses a MVC approach. Having to unlearn bad habits while learning completely new material causes some confusion.

EDIT: Reread the page... nope. His explanation still doesn't make sense.
 
From a procedural to objective transition, the thing to remember about objects is that they are responsive, not proactive. You create your objects and run some setup code in -awakeFromNib or the equivalent and the objects respond to events as they occur (as messages come in). So, basically, a typical Cocoa application will just lie there until you make it do something. The only way things will happen without some action on the part of the user or other input (such as network data coming in) is if you start a NSTimer and connect it to a object/action-method to make things happen on a regular basis. Basically, you are moving from your program polling and doing stuff to a bunch of interlinked objects waiting for stuff to do.
 
Don't know if you solved it already, but as I'm using the same book, I thought I'd point out what I did. At the end of your Add method, just put

Code:
	[tableView reloadData];
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.