Rajeev Rajeev -3 years ago 43
iOS Question

issue with getting the data from url objective c

I have a issue with the below service

{
"DataTable": [
{
"EmpTable": [
{
"Name": "Rakesh",
"Finaldata": "5",
"data": "One Year Free",
"heading": "HR",
},
{
"Name": "Roshan",
"Finaldata": "1",
"data": "1 Month",
"heading": "Software",

},
{
"Name": "Ramesh",
"Finaldata": "5",
"data": "3 Month",
"heading": "Admin",
},

]
}
]
}


Only getting the details of Ramesh from the above output, remaining data doesn't display in my table view. Below is my code what i have tried from the above service. Please help to find out the issue. TIA

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return _empArr.count;
}


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


EmpCell *cell = (MembershipCell *) [tableView dequeueReusableCellWithIdentifier:@"Cell"];

if (cell == nil) {

NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"MembershipCell" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (EmpCell *) currentObject;

}
}
}

profiledict = [_empArr objectAtIndex:indexPath.row];

for (NSDictionary *temp in profiledict) {

cell.lblName.text = [temp objectForKey:@"Name"];
cell.lblFinaldata.text = [temp objectForKey:@"Finaldata"];
cell.lbldata.text = [temp objectForKey:@"data"];
cell.lblheading.text = [temp objectForKey:@"heading"];

}

return cell;
}



- (void)jsonData:(NSDictionary *)jsonDict
{
NSMutableArray *jsonArr;
NSMutableDictionary *dict;
[SVProgressHUD dismiss];


jsonArr=[jsonDict objectForKey:@"DataTable"];

if (![jsonArr isEqual:[NSNull null]]) {
_empArr=[[NSMutableArray alloc] init];

for (int i=0; i<jsonArr.count; i++) {
dict=[jsonArr objectAtIndex:i];
[_empArr addObject:[dict objectForKey:@"EmpTable"]];


}
[self.tableView reloadData];
}
else
{


[SVProgressHUD showErrorWithStatus:@"Something went wrong"];

[self.tableView reloadData];

}
}

Answer Source

You are adding Whole EmpTable array as an object in array. So there is only one object in array. Thats why only one cell will be added on tableView. Try to extract array objects from EmpTable array.

In - (void)jsonData:(NSDictionary *)jsonDict method

Replace

[_empArr addObject:[dict objectForKey:@"EmpTable"]];

with

[_empArr addObjectsFromArray:[dict objectForKey:@"EmpTable"]];

and in cellForRowAtIndexPath

Replace

profiledict = [_empArr objectAtIndex:indexPath.row];

for (NSDictionary *temp in profiledict) {

    cell.lblName.text = [temp objectForKey:@"Name"];
    cell.lblFinaldata.text = [temp objectForKey:@"Finaldata"];
    cell.lbldata.text = [temp objectForKey:@"data"];
    cell.lblheading.text = [temp objectForKey:@"heading"];

}

With

profiledict = [_empArr objectAtIndex:indexPath.row];
cell.lblName.text = [profiledict objectForKey:@"Name"];
cell.lblFinaldata.text = [profiledict objectForKey:@"Finaldata"];
cell.lbldata.text = [profiledict objectForKey:@"data"];
cell.lblheading.text = [temp objectForKey:@"heading"];

Hope this helps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download