【问题标题】:How can I display all object values in one NSTableCellView?如何在一个 NSTableCellView 中显示所有对象值?
【发布时间】:2026-02-08 07:45:01
【问题描述】:

我有一个 Product 对象,但现在我只能在每个表格单元格视图中打印它的一个值。我想在一个单元格中显示我的对象的所有内容。当我尝试添加另一个值时,它只会抓取我创建的最后一个 cellView。如何用我的所有对象值填充我的单元格?

#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

【问题讨论】:

    标签: objective-c cocoa nstableview nstablecellview


    【解决方案1】:

    万一有人关心...您必须创建一个自定义单元格并提供自定义单元格类标签并将对象的值分配给标签。确保将新的自定义类导入视图控制器。现在,每个单元格都可以使用数据库/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
    

    表格视图

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

    【讨论】:

      最近更新 更多