2011-02-23 13 views
0

我正在做一個關於NSMutableDictionary的小概念。 我有四個鍵(姓名,年齡,電話號碼,性別)爲10名員工。使用MultipleKeys存儲在SingleArray中的可變字典

如何將這些值分配給數組?

單獨爲字典傳遞了4個不同的值,但第四個值重複出現。 這是代碼我所寫的 - (無效)viewDidLoad中{

NSLog(@"in mytableviews viewdidload"); 



    EmpArray=[[NSMutableArray alloc]init]; 





//namesArray=[[NSMutableArray alloc]initWithObjects:@"jam",@"jack",@"gillchrist",@"jackson"]; 


    EmpDictionary=[[NSMutableDictionary alloc]init]; 
[EmpDictionary setValue:@"martin" forKey:@"name"]; 
[EmpDictionary setValue:@"18" forKey:@"age"]; 
[EmpDictionary setValue:@"M" forKey:@"gender"]; 
[EmpDictionary setValue:@"9652893070" forKey:@"phoneNumber"]; 
    [EmpArray addObject:EmpDictionary]; 
NSLog(@"emparray counr %d",[self.EmpArray count]); 



[EmpDictionary setValue:@"jack" forKey:@"name"]; 
[EmpDictionary setValue:@"19" forKey:@"age"]; 
[EmpDictionary setValue:@"F" forKey:@"gender"]; 
[EmpDictionary setValue:@"96656893070" forKey:@"phoneNumber"]; 
[EmpArray addObject:EmpDictionary]; 

[EmpDictionary setValue:@"Louis" forKey:@"name"]; 
[EmpDictionary setValue:@"21" forKey:@"age"]; 
[EmpDictionary setValue:@"F" forKey:@"gender"]; 
[EmpDictionary setValue:@"9652893060" forKey:@"phoneNumber"]; 
[EmpArray addObject:EmpDictionary]; 

[EmpDictionary setValue:@"Gillchrist" forKey:@"name"]; 
[EmpDictionary setValue:@"23" forKey:@"age"]; 
[EmpDictionary setValue:@"M" forKey:@"gender"]; 
[EmpDictionary setValue:@"99998989" forKey:@"phoneNumber"]; 
[EmpArray addObject:EmpDictionary]; 


[super viewDidLoad]; 

} 

- (UITableViewCell的*)的tableView:(UITableView的*)的tableView的cellForRowAtIndexPath:(NSIndexPath *)indexPath {

NSLog(@"creating cell in cell for row at index"); 

employeeCell *empcell = (employeeCell *)[tableView dequeueReusableCellWithIdentifier:@"empcell1"]; 

if(empcell==nil) 
{ 
    NSLog(@"In creating cell"); 
    [[NSBundle mainBundle]loadNibNamed:@"employeeCell" owner:self options:nil]; 
    empcell=tableCell; 
} 

for(int i=1;i<=3;i++) 
{ 

    NSLog(@"after creating cell"); 

    //NSDictionary *temp=[self.EmpArray objectAtIndex:indexPath.row]; 
NSDictionary *temp = [self.EmpArray objectAtIndex:indexPath.row]; 
NSString *empname= [temp objectForKey:@"name"]; 
NSString *age= [temp objectForKey:@"age"]; 
//NSInteger age = [temp objectForKey:@"age"]; 
NSString *gender= [temp objectForKey:@"gender"]; 
NSString *phonenumber = [temp objectForKey:@"phoneNumber"]; 

empcell.EmpName.text=empname; 

empcell.EmpAge.text=age; 

empcell.EmpGender.text=gender; 
empcell.EmpPhoneNumber.text=phonenumber; 


empcell.accessoryType=UITableViewCellAccessoryDisclosureIndicator; 
} 
return empcell; 

}

+0

重複來意是什麼意思?所以你想要一個字典數組? – 2011-02-23 14:33:03

+0

謝謝奧利修改問題。請給它建議。謝謝你再次 – girish 2011-02-23 14:33:17

+0

我的意思是反覆意味着在每個單元格的表格視圖中,我會得到相同的值(Smith,1244,London,05897469878)。這些是最後一個值對於字典,我已經通過了編碼。請爲它提供幫助.thanku爲我的問題提供迴應。 – girish 2011-02-23 14:49:17

回答

1

您將要爲要添加的每組條目創建新詞典

[[EmpArray addObject:[EmpDictionary copy]]; 

否則,你只是添加同一個對象4次,所以你的最終結果顯示4個單元格中有相同的信息。你的結束數組不是4個不同的對象,只有4個指針指向同一個對象。