1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Input and Tableview functions, Hillegass book chapter 6

Discussion in 'Mac Programming' started by nick.hobbie, Sep 3, 2008.

  1. macrumors newbie

    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;
    @implementation AppController
    - (id)init
    	[super init];
    	//Logs used for debugging 
    	toDoList =[[NSMutableArray alloc] init];
    	return self; 
    	NSString *string = [textField stringValue];
    	//is the string zero length? 
    	if([string length] == 0)
    		NSLog(@"string from %@ is of zero length",textField);
    	[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 
    	NSString *v = [toDoList objectAtIndex:row];
    	NSLog(@"string from List%@",v);
    	return v; 
    and all my connections using interface builder
  2. macrumors newbie

    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.
  3. Moderator emeritus


    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 [color=red][b]%d[/b][/color]",[toDoList count]);
  4. Guest


    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?
  5. Moderator emeritus


    According to here, there isn't a difference. You can use %d, %D, or %i for a signed 32-bit integer (long).
  6. Guest


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


Share This Page