PDA

View Full Version : Input and Tableview functions, Hillegass book chapter 6




nick.hobbie
Sep 3, 2008, 04:35 AM
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.

#import <Cocoa/Cocoa.h>


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


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




@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
http://www.students.ipfw.edu/~hobbnr01/Macrumors.jpg



R Shuff
Sep 22, 2008, 12:04 PM
Add the following to your method "add:"

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

kainjow
Sep 22, 2008, 01:32 PM
This is the line that's crashing:
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:

NSLog(@"number of list %d",[toDoList count]);

Darkroom
Sep 22, 2008, 04:33 PM
This is the line that's crashing:
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:

NSLog(@"number of list %d",[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
Sep 22, 2008, 04:44 PM
According to here (http://developer.apple.com/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html), there isn't a difference. You can use %d, %D, or %i for a signed 32-bit integer (long).

Darkroom
Sep 22, 2008, 04:57 PM
According to here (http://developer.apple.com/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html), there isn't a difference. You can use %d, %D, or %i for a signed 32-bit integer (long).

ah... "String Format Specifiers"... that's a much better title than my "Conversion Characters" :p

thanks...