【问题标题】:NSArray from MySQL to NSTableViewNSArray 从 MySQL 到 NSTableView
【发布时间】:2012-04-02 13:56:50
【问题描述】:

首先,我是 Objective-c 编程的新手,所以我可能在我的代码中犯了一些错误。

这是我所做的:我创建了一个带有一些 xml 的 php 文件,以从我的 MySQL 数据库中获取值。一切都进入objective-C中的NSArray。如果我查看日志,一切正常,因为它显示了我的数组,其中包含每个 MySQL 行和列,如下所示:

2012-04-02 08:20:14.822 POS[64632:707] (

    {
    CPU = 0;
    TPS = "(null)";
    TVQ = "(null)";
    consigne = 0;
    coutantQuantiteRecue = 0;
    description = "";
    nom = "";
    prixProduit = 0;
    quantiteRecue = 0;
    stock = 0;
},
    {
    CPU = 768;
    TPS = "(null)";
    TVQ = "(null)";
    consigne = 0;
    coutantQuantiteRecue = 0;
    description = "";
    nom = hhh;
    prixProduit = 0;
    quantiteRecue = 0;
    stock = 0;
},

2012-04-02 08:20:14.836 POS[64632:707] The number of rows is:2

问题是我在 TableView 中得到的唯一内容是每个单元格中的 {,如下所示:

这是我的 .h :

@interface InventoryManagement : NSObject<NSApplicationDelegate, NSTableViewDataSource>
{
IBOutlet NSTableView* inventoryTable;
NSArray* m_items;

}
-(int)numberOfRowsInTableView:(NSTableView *)inventoryTable;
-(id)tableView:(NSTableView *)inventoryTable objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)rowIndex;
-(void)dealloc;

@property (retain) NSArray* m_items;

@end

这是我的 .m 文件:

@implementation InventoryManagement

@synthesize m_items;

-(id)init
{
[super init];

NSInteger index = 0;
NSString *urlString = [NSString stringWithFormat:@"http://localhost:8888/php/getProducts.php?index=%d&", index];
m_items = [NSArray arrayWithContentsOfURL:[NSURL URLWithString: urlString]];
NSLog(@"%@", [m_items description]);
[m_items retain];
[inventoryTable reloadData];
return self;
}



-(int)numberOfRowsInTableView:(NSTableView *)inventoryTable
{

NSLog(@"The number of rows in fullResult is:%i", [m_items count]);
return [m_items count];



}

-(id)tableView:(NSTableView *)inventoryTable objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)rowIndex
{

return [m_items objectAtIndex:rowIndex];

}

-(void)dealloc
{
[m_items release];
[super dealloc];
}

@end

我的对象很好地连接到我的 TableView 的数据源和委托。我希望这些单元格由我的数据库值填充。

【问题讨论】:

    标签: mysql objective-c cocoa nsarray nstableview


    【解决方案1】:

    看看 Cocoa Bindings 以了解未来的工作。他们很棒。

    关于你的问题:问题出在方法上

    -(id)tableView:(NSTableView *)inventoryTable objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)rowIndex
    {
    
    return [m_items objectAtIndex:rowIndex];
    
    }
    

    您正在返回整个对象,即:

    { CPU = 768; TPS =“(空)”; TVQ = "(null)"; 收货人 = 0; coutantQuantiteRecue = 0; 描述 = ""; 标称 = hhh; prixProduit = 0; 数量恢复 = 0; 库存 = 0; }

    这是一个字符串。 您必须检查 tableColumn 变量并仅返回正确的信息:例如quantiteRecue、CPU 等。

    最好的方法是在 ObjectiveC 中创建一个 Model 类来包装你的 db 的一行。然后您的 m_items 数组将包含您的 Model 类,并且您可以返回正确的属性而无需每次解析字符串...

    【讨论】:

    • 你是对的。我没有意识到。我会试一试,稍后再发布我的结果。谢谢!
    • 我实际上做的是创建一个“for”循环并将每个列单元格插入一个字符串[],然后另一个循环将这些字符串插入一个数组[]。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多