2016-09-21 102 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

完美無缺。非常感謝! –