0

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)

4

3 に答える 3

0

テーブルにロードしたアイテムがテーブル内で同じ順序である場合は、次を使用できます。

    [myArray objectAtIndex:[indexPath row]];

次に、useは、必要な配列の変数を使用できます。

于 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];
   }

Customer *Customer を Customer *customer に変更する 常にその方法を使用します。

次に、その方法を使用して、どの行が選択されているかを理解します。あなたの場合、どの顧客が選択されていますか。あなたのコードから、すべての行に貸衣装があり、それらの顧客がリスト配列にあることがわかります。

    - (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 に答える