2016-09-21 105 views
0

我对Objective-C相当陌生,我发现iOS和UITableView有很多教程,但通过NSTableView几乎没有用于OS X Apps的教程。我建立了一种方法来检索我的数据,但在最后一行发现错误:如何重新加载数据到NSTableView?

“在对象类型ProductsViewController上未找到属性tableView”。

我不知道正确的方式重新加载我的数据到我的表,或者如果我甚至需要使用NSTableView这个特定的实例吗?有没有比使用NSTableView更好的显示数据的方式?

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

#define getDataURL @"http://myurl" 


@interface ProductsViewController() 

@end 

@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]; 


} 
+0

当然,你需要一个'NSTableView'和一些更多的数据源/委托方法或Cocoa绑定。 – vadian

回答

1

您需要为NSTableViewDataSource协议实施所需的代理方法。具体来说,你需要这两个:

numberOfRowsInTableView: 
tableView:objectValueForTableColumn:row: 

表视图然后将调用这些方法的所需数据。

此外,在raywenderlich.com上有关于使用NSTableViews的a great tutorial

+0

完美无缺。非常感谢! –