我有一个程序,在两个不同的窗口上有 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)