Nick Balistreri Nick Balistreri - 11 months ago 41
Objective-C Question

How to reload data into NSTableView?

I am fairly new to Objective-C and I find there are a lot of tutorials for iOS and UITableView but almost none for OS X Apps via NSTableView. I have built a method to retrieve my data but I get an error on the last line:

"Property tableView not found on object type ProductsViewController".

I do not know the correct way to reload my data into my table or if I even need to use an NSTableView for this specific instance? Is there a better way to display my data than using NSTableView?

#import "ProductsViewController.h"
#import "Product.h"

#define getDataURL @"http://myurl"

@interface ProductsViewController ()


@implementation ProductsViewController

@synthesize jsonArray, productsArray;

- (void)viewDidLoad {
[super viewDidLoad];

[self retrieveData];


-(NSInteger)numberOfRowsInTable:(NSTableView *)tableView{

return productsArray.count;

- (void) retrieveData{

NSURL * url = [NSURL URLWithString:getDataURL];
NSData * data = [NSData dataWithContentsOfURL:url];

jsonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

productsArray = [[NSMutableArray alloc] init];

for(int i = 0; i < jsonArray.count; i++){

NSString * pID = [[jsonArray objectAtIndex:i] objectForKey:@"id"];
NSString * pName = [[jsonArray objectAtIndex:i] objectForKey:@"product_name"];
NSString * pPrice = [[jsonArray objectAtIndex:i] objectForKey:@"product_price"];
NSString * pDescription = [[jsonArray objectAtIndex:i] objectForKey:@"product_description"];
NSString * pImage = [[jsonArray objectAtIndex:i] objectForKey:@"product_image"];
NSString * pDownload = [[jsonArray objectAtIndex:i] objectForKey:@"product_download"];
NSString * pVideo = [[jsonArray objectAtIndex:i] objectForKey:@"product_video"];
NSString * pFeatured = [[jsonArray objectAtIndex:i] objectForKey:@"featured"];

[productsArray addObject:[[Product alloc] initWithProduct_Name: pName andProduct_Price:pPrice andProduct_Description:pDescription andProduct_Image:pImage andProduct_Download:pDownload andProduct_Video:pVideo andProduct_Featured:pFeatured andProduct_ID:pID]];

[self.tableView reloadData];


Answer Source

You need to implement the required delegate methods for the NSTableViewDataSource protocol. Specifically, you need these two:


The table view will then call these methods for the data it wants.

In addition, there's a great tutorial over at about using NSTableViews.