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

nick.hobbie

macrumors newbie
Original poster
Oct 4, 2007
24
0
I am having problems in Hillegass book, chapter 6, the very last page, Challenge: make a data source. Pretty much, my table view is not updating any of the data. and when I do [tableView reloadData] my program crashes.

The basic premis is that you have an input field and an add button. What ever is in the input field gets added to the list and displayed in tableView.

Here is my code.
Code:
#import <Cocoa/Cocoa.h>


@interface AppController : NSObject {
	IBOutlet NSTextField *textField;
	IBOutlet NSButton *addButton;
	IBOutlet NSTableView *tableView;
	NSMutableArray *toDoList;
}


- (IBAction)add:(id)sender;
@end

Code:
@implementation AppController
- (id)init
{
	[super init];
	//Logs used for debugging 
	NSLog(@"init");
	toDoList =[[NSMutableArray alloc] init];
	return self; 
}

-(IBAction)add:(id)sender
{
	NSString *string = [textField stringValue];
	//is the string zero length? 
	if([string length] == 0)
	{
		NSLog(@"string from %@ is of zero length",textField);
		return;
	}
	[toDoList addObject: (id)string];
	NSLog(@"string from %@",toDoList);
}

- (int)numberOfRowsInTableView:(NSTableView *)tv
{
	NSLog(@"number of list %@",[toDoList count]);
	return [toDoList count];
	

}

-(id)tableView:(NSTableView *)tv 
objectValueForTableColumn:(NSTableColumn *)tableColumn 
		   row:(int)row
{
	NSString *v = [toDoList objectAtIndex:row];
	NSLog(@"string from List%@",v);

	return v; 
}
@end

and all my connections using interface builder
Macrumors.jpg
 

R Shuff

macrumors newbie
Sep 22, 2008
1
0
Re: I think you just need to reload the data source

Add the following to your method "add:"

[tableView reloadData]; //Reloads datasource back into tableview.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
This is the line that's crashing:
Code:
NSLog(@"number of list %@",[toDoList count]);

The reason it's crashing is because you're using %@, which expects an object, but [toDoList count] returns a primitive. Use %d instead:

Code:
NSLog(@"number of list [color=red][b]%d[/b][/color]",[toDoList count]);
 

Darkroom

Guest
Dec 15, 2006
2,445
0
Montréal, Canada
This is the line that's crashing:
Code:
NSLog(@"number of list %@",[toDoList count]);

The reason it's crashing is because you're using %@, which expects an object, but [toDoList count] returns a primitive. Use %d instead:

Code:
NSLog(@"number of list [color=red][b]%d[/b][/color]",[toDoList count]);

sorry OP for the thread hijack, but now that kainjow has mentioned it...

what's the actual difference between the conversion characters %d and %i? i mean, at first i would use %d, but then when i went back and studied C and Objective-C i started using %i...

is there a difference between these conversions?
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
According to here, there isn't a difference. You can use %d, %D, or %i for a signed 32-bit integer (long).
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.