NSmutable 字典,具有多个键存储在 SingleArray 中

发布于 2024-10-19 09:13:20 字数 2626 浏览 1 评论 0原文

我正在 NSMutableDictionary 上做一个小概念。 我有 10 名员工的四个键(姓名、年龄、电话号码、性别)。

如何将这些值分配给数组?

我分别为字典传递了 4 个不同的值,但第四个值重复出现。 这是我写的代码 - (void)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;

}

I am doing a small concept on NSMutableDictionary.
I have four keys (Name, Age, PhoneNumber, Gender) for 10 employees.

How can I assign these values to an array?

Individually I passed 4 different values for the dictionary but the fourth value is coming repeatedly.
this is the code what i have written
- (void)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;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绻影浮沉 2024-10-26 09:13:20

您需要为要添加的每组条目创建新字典 否则

[[EmpArray addObject:[EmpDictionary copy]];

,您只需添加相同的对象 4 次,因此最终结果显示 4 个单元格其中的信息相同。你的最终数组不是 4 个不同的对象,只是指向同一个对象的 4 个指针。

You're going to want to either create new dictionaries for each set of entries you want to add or do

[[EmpArray addObject:[EmpDictionary copy]];

Otherwise, you're just adding the same object 4 times, so your end result show 4 cells with the same information in them. Your end array is not 4 different objects, just 4 pointers to the same object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文