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

    #1
    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
    [​IMG]
     
  2. macrumors newbie

    #2
    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

    kainjow

    #3
    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]);
     
  4. Guest

    Darkroom

    #4
    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

    kainjow

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

    Darkroom

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

    thanks...
     

Share This Page