2016-09-23 1 views
-1

J'ai un objet Product mais pour le moment je ne peux imprimer qu'une seule de ses valeurs dans chaque vue de cellule de tableau. Je veux afficher tout le contenu de mon objet dans une cellule. Lorsque j'essaie d'ajouter une autre valeur, elle saisit uniquement la dernière cellule que je crée. Comment puis-je remplir ma cellule avec toutes mes valeurs d'objet?Comment puis-je afficher toutes les valeurs d'objet dans un NSTableCellView?

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


@interface ProductsViewController() <NSTableViewDataSource, NSTableViewDelegate> 

@end 

@implementation ProductsViewController 

@synthesize jsonArray, productsArray; 


- (IBAction)tableView:(NSTableView *)sender { 


} 


- (void)viewDidLoad { 
[super viewDidLoad]; 


[self retrieveData]; 



} 


- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView 
{ 
return [self.productsArray count]; 
} 


- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row 
{ 
static NSString *cellIdentifier = @"cell"; 

NSTableCellView *cellView = [tableView makeViewWithIdentifier:cellIdentifier owner:self]; 

if (cellView == nil) 
{ 
    NSLog(@"A cell"); 

    cellView = [[NSTableCellView alloc] init]; 
    [cellView setIdentifier:cellIdentifier]; 
} 
Product * productObject; 
productObject = [productsArray objectAtIndex:row]; 


cellView.textField.stringValue = productObject.product_name; 

return cellView; 
} 



- (void) retrieveData{ 

NSString * url = @"myurl"; 

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

NSString *DataResult = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 

NSLog(@"%@",DataResult); 


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




} 

@end 

Répondre

0

Dans le cas où quelqu'un se soucie ... Vous devez créer une cellule personnalisée et donner à vos étiquettes de classe de cellules personnalisées et affecter les valeurs de votre objet aux étiquettes. Assurez-vous d'importer votre nouvelle classe personnalisée dans votre contrôleur de vue. Maintenant, chaque cellule peut être personnalisée avec autant d'étiquettes, d'images et de contenu de votre base de données/API/JSON.

Custom.h

#import <Cocoa/Cocoa.h> 

@interface Custom : NSTableCellView 

@property (weak) IBOutlet NSTextField *label1; 

@property (weak) IBOutlet NSTextField *label2; 

@end 

Custom.m

#import "Custom.h" 

@implementation Custom 

@synthesize label1, label2; 

@end 

TableView

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row 
{ 
static NSString *cellIdentifier = @"cell"; 

Custom *cellView = [tableView makeViewWithIdentifier:cellIdentifier owner:self]; 

if (cellView == nil) 
{ 
    NSLog(@"A cell"); 

    [cellView setIdentifier:cellIdentifier]; 
} 



Product * productObject; 
productObject = [productsArray objectAtIndex:row]; 

cellView.textField.stringValue = productObject.product_image; 
cellView.label1.stringValue = productObject.product_name; 
cellView.label2.stringValue = productObject.product_price; 




return cellView; 
}