Adding new item to NSTableView

Discussion in 'Mac Programming' started by Samppaa, May 29, 2010.

  1. Samppaa macrumors regular

    Joined:
    Mar 26, 2010
    #1
    Hello I am currently making my first mac app and I would like to know how can I add item to the NSTableview I already made the datasource thing here is the code

    TableDataSource.h

    Code:
    //
    //  TableDataSource.h
    //  Macohjelma
    //
    //  Created by Samuli Lehtonen on 29.5.2010.
    //  Copyright 2010 Test. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    
    @interface TableDataSource : NSObject {
    	
    	@private
    	NSArray *aBuffer;
    
    }
    
    @end
    
    TableDataSource.m
    Code:
    //
    //  TableDataSource.m
    //  Macohjelma
    //
    //  Created by Samuli Lehtonen on 29.5.2010.
    //  Copyright 2010 Test. All rights reserved.
    //
    
    #import "TableDataSource.h"
    
    
    @implementation TableDataSource
    
    
    - (id)     tableView:(NSTableView *) aTableView
    objectValueForTableColumn:(NSTableColumn *) aTableColumn
    				 row:(int) rowIndex
    {  
    	return [aBuffer objectAtIndex:rowIndex];  
    }
    
    
    - (int)numberOfRowsInTableView:(NSTableView *)aTableView
    {
    	return [aBuffer count];
    }
    
    @end
    
    And here is the code where I try to add the item,
    Code:
    [Notes setDataSource:DataSource];
    [DataSource addItemWithTitle:@"Hello"];
    [Notes reloadData];
    
    I was wondering whats wrong as I don't see it there when I try to add, do I have to implement the additemwithtitle method inside my datasource class?
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    Post more code.

    You don't show how your variables Notes and DataSource are declared.

    You don't show how the aBuffer ivar in an instance of TableDataSource is assigned a value. Unless and until you assign it a value, then that ivar will be nil.

    You don't show the definition of any method addItemWithTitle:. Why would you expect it to do anything if you don't define a method that does something?

    If you're following a tutorial or a book, tell us exactly what it is. If you're not, you probably should be, because you're missing a whole lot of really important things.
     
  3. Samppaa thread starter macrumors regular

    Joined:
    Mar 26, 2010
    #3
    v

    I guess I just download a book and follow it as this is amazingly hard compared what I have done before on windows development. I guess it starts to make sense sometime...
     
  4. fernandovalente macrumors 6502

    fernandovalente

    Joined:
    Aug 29, 2008
    Location:
    Third world **** hole, I mean, Brazil
    #4
    Show us your code. But let me try to understand what you're trying to do.

    If DataSource is a NSMutableArray, it's
    Code:
    [DataSource addObject:@"Hello"];
    Not
    Code:
    [DataSource addItemWithTitle:@"Hello"];
    Also, I think you're trying to set the datasource for the same class you're calling those methods from, so it's:
    Code:
    [Notes setDataSource:self];
    Not
    Code:
    [Notes setDataSource:DataSource];
    But really, unless you post your entire code, we will not be able to help you ;)
     
  5. BadWolf13 macrumors 6502

    Joined:
    Dec 17, 2009
    #5
    It's actually quite simple. I'm assuming that "Notes" is your outlet for the tableView and that "DataSource" is your array. First thing is that fernandovalente is right, addItemWithTitle: is not a valid method for an NSMutableArray, what you're looking for is addItem:. Second, I'm not sure if it's a big deal, but object names should start with a lower case letter, which you did not follow with DataSource and Notes. Your program may not be able to access the setter because of this small difference.
     
  6. Samppaa thread starter macrumors regular

    Joined:
    Mar 26, 2010
    #6
    Can you tell me good book what teaches all these things and gives some exercises... Seems so hard to learn alone
     
  7. fernandovalente macrumors 6502

    fernandovalente

    Joined:
    Aug 29, 2008
    Location:
    Third world **** hole, I mean, Brazil
    #7
  8. CarlJ macrumors 68020

    CarlJ

    Joined:
    Feb 23, 2004
    Location:
    San Diego, CA, USA
    #8
  9. BadWolf13 macrumors 6502

    Joined:
    Dec 17, 2009
    #9
    I concur with Aaron Hillegass' book. It's what I am using, and I find it very good. I'm trying to be good and actually read the whole thing before I bug everyone on here with silly questions, but so far I'm not succeeding. :D
     

Share This Page