2 つの異なるウィンドウに 2 つのテーブルを持つプログラムがあります。1 つのテーブルには顧客名と ID 番号が保持され、もう 1 つのテーブルにはアイテム名と番号が保持されます。それらは両方とも配列と .plist ファイルに格納されます。
私がやりたいことは、3 番目のページに、ユーザーが顧客 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
これは顧客です。
#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)