objective c - NSmutable Dictionary with MultipleKeys to Store in SingleArray -
i doing small concept on nsmutabledictionary. have 4 keys (name, age, phonenumber, gender) 10 employees.
how can assign these values array?
individually passed 4 different values dictionary fourth value coming repeatedly. code 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 row @ 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;
}
you're going want either create new dictionaries each set of entries want add or do
[[emparray addobject:[empdictionary copy]];
otherwise, you're adding same object 4 times, end result show 4 cells same information in them. end array not 4 different objects, 4 pointers same object.
Comments
Post a Comment