2012-04-13 61 views
0

我有一個程序在兩個不同的窗口上有兩個表。一個表格包含客戶名稱和ID號碼,另一個表格包含物品名稱和編號。它們都存儲在一個數組中,也存儲在.plist文件中。如何從ID號碼獲取對象實例的信息

我想要做的是在第三頁上將出現一個銷售頁面,用戶將輸入客戶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 

這是customer.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)

回答

0

例如:

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

這檢查對象mynumber的是一個NSNumber的類對象。當然,你可以使用任何你想要的課程。閱讀documentation

+0

因此,如果我理解正確,讓我的程序顯示一個客戶的名稱只有一個ID號我會做你說的話,然後在方法位我會說[標籤setValue名稱]; ? – GBSingh 2012-04-13 08:31:50

+0

等等......你是用id表示的? http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/objectivec/Chapters/ocObjectsClasses.html對象標識符或您的internat客戶ID? – Kuba 2012-04-13 08:38:26

+0

我的意思是給每位顧客一個ID號碼 – GBSingh 2012-04-13 08:45:53

0

如果您加載到表中的項目是在你的表相同的順序,你可以使用

[myArray objectAtIndex:[indexPath row]]; 

然後使用可以使用您所需要的數組變量。

+0

對不起,但我是一個完整的初學者,所以我不知道我必須做什麼。我做一個新的對象和類,然後將它們連接起來。我有什麼鏈接到界面。我將把客戶類代碼和表格數據源代碼放在一起,以便您可以看到我正在嘗試執行的操作。謝謝 – GBSingh 2012-04-13 09:13:09

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

    } 

然後您可以達到客戶的價值。

+0

嗨。感謝你的回答。然而,當我做你ü嘗試我得到2錯誤。一個是:未找到實例方法'-row'(返回類型默認爲'id'),另一個是:不兼容指向整數轉換的指針,將'id'發送給類型爲'NSUInteger'(又名'unsigned long')的參數。還有一次,我已經這樣做了,我如何打印客戶的名稱取決於他們的ID號碼(memberNumber)。另一個這謝謝你告訴我,我應該做客戶*客戶,但如果你不介意我的問題,但爲什麼你這樣做?這是否只是約定?謝謝 – GBSingh 2012-04-13 11:02:45

+0

瘦你想獲得客戶memberId。 ın我的案例代碼將customer.memberId,但在您的casa Customer.memberId這將導致一個錯誤。它可以像一個單身課程一樣行事,你不能得到正確的價值。也嘗試將[indexPath行]更改爲indexPath.row – mhunturk 2012-04-13 11:11:31

+0

這是iOS或OS X的答案,因爲我認爲UITableView適用於iOS – GBSingh 2012-04-13 11:23:07