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

Samppaa

macrumors regular
Original poster
Mar 26, 2010
168
23
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?
 
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.
 
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...
 
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?

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 ;)
 
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.
 
Can you tell me good book what teaches all these things and gives some exercises... Seems so hard to learn alone
 
As an Amazon Associate, MacRumors earns a commission from qualifying purchases made through links in this post.
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.