0

我有一个程序,在两个不同的窗口上有 2 个表。一张表保存客户姓名和 ID 号,另一张保存项目名称和编号。它们既存储在数组中,也存储在 .plist 文件中。

我想做的是在第三页上会有一个销售页面,用户将在其中输入客户 ID 和项目 ID,程序应该能够找到名称并将其显示到标签上。我不知道从哪里开始。有人可以帮忙或告诉我该怎么做吗?我可以上传任何人想看到的任何代码,但由于我不知道从哪里开始,我不知道要上传什么。
这是 customer.h 文件

#import <Foundation/Foundation.h>
NSString *name;
int memberNumber;


@interface Customer : NSObject <NSCoding>
{
NSString *name;
int memberNumber;

}
@property (nonatomic, copy) NSString *name;
@property int memberNumber;


@end  

这是客户.m

#import "Customer.h"

@implementation Customer

@synthesize name;
@synthesize memberNumber;


-(id) init
{
self = [super init];
if(self) 
    {
    name = @"Test";
    int i = arc4random()%1000000000000000000;
    if (i<0) 
        {
        memberNumber = i*-1;
        }
    else
        memberNumber = i;
}
return self;

}

- (id)initWithCoder:(NSCoder *)decoder 
{
if (self = [super init]) 
{
    self.name = [decoder decodeObjectForKey:@"name"];
    self.memberNumber = [decoder decodeIntForKey:@"memberNumber"];


}
return self;
}

- (void)encodeWithCoder:(NSCoder *)encoder 
{
[encoder encodeObject:name forKey:@"name"];
[encoder encodeInt:memberNumber forKey:@"memberNumber"];

}

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


@end

这是 tableView.h 文件

#import <Foundation/Foundation.h>
#include <stdlib.h>
NSString *filepath; 
@interface tableViewData : NSObject <NSTableViewDataSource> 
{
@private
    IBOutlet NSTableView *tableView;
    NSMutableArray *list;
    NSString *filepath; 




}
-(IBAction)add:(id)sender;
-(IBAction)remove:(id)sender;





@end

这是 tableView.m 文件

#import "tableViewData.h"
#import "Customer.h"
@implementation tableViewData

-(void)awakeFromNib{
filepath = @"/Users/Desktop/CustomerNames.plist";
if ([[NSFileManager defaultManager]fileExistsAtPath:filepath]) 
{
    NSMutableArray *archive = [NSKeyedUnarchiver unarchiveObjectWithFile:filepath];
    list = archive;
}
else
    list=[[NSMutableArray alloc]init];
}

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

-(id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
Customer *Customer = [list objectAtIndex:row];
NSString *identifier = [tableColumn identifier];
return [Customer valueForKey:identifier];
}

-(void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:     (NSTableColumn *)tableColumn row:(NSInteger)row
{
Customer *Customer = [list objectAtIndex:row];
NSString *identifier = [tableColumn identifier];
[Customer setValue:object forKey:identifier];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:list];
[data writeToFile:filepath options:NSDataWritingAtomic error:nil];
}



-(IBAction)add:(id)sender
{
[list addObject:[[Customer alloc]init]];
[tableView reloadData];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:list];
[data writeToFile:filepath options:NSDataWritingAtomic error:nil];
for (id name in list)
    NSLog(@"obj: %@", name);
NSLog (@"array:%@",list);


}


-(IBAction)remove:(id)sender
{
NSInteger row = [tableView selectedRow];
if (row != -1) 
{
    [list removeObjectAtIndex:row];
}

[tableView reloadData];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:list];
[data writeToFile:filepath options:NSDataWritingAtomic error:nil];

}




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


@end

希望这可以帮助

(用于 OS X 的 xcode 4.2.1)

4

3 回答 3

0

If items you loaded to table are at the same order in your table you can use

    [myArray objectAtIndex:[indexPath row]];

Then use can use the variables of the array that you needed.

于 2012-04-13T08:08:52.840 回答
0

首先在该代码中:

   -(void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:                   (NSTableColumn *)tableColumn row:(NSInteger)row
          {
   Customer *Customer = [list objectAtIndex:row];
   NSString *identifier = [tableColumn identifier];
   [Customer setValue:object forKey:identifier];
   NSData *data = [NSKeyedArchiver archivedDataWithRootObject:list];
   [data writeToFile:filepath options:NSDataWritingAtomic error:nil];
   }

将客户*客户更改为客户*客户始终使用这种方式。

然后使用该方法来了解选择了哪一行。在您的情况下,选择了哪个客户。我从您的代码中了解到,每一行都有一个客户,这些客户在您的列表数组中。

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
      //with this code you can get the selected customer from your list,
      Customer *customer=[list objectAtIndex:[indexPath row]];

    }

然后你可以达到客户的价值。

于 2012-04-13T10:27:25.200 回答
0

例如:

if([myNumber isKindOfClass:[NSNumber class]])
{
    //do something here
}

检查对象 myNumber 是否是 NSNumber 类对象。当然,你可以使用任何你想要的类。阅读文档

于 2012-04-13T07:49:13.707 回答