从url获取数据的问题目标C

问题描述:

我对以下服务存在问题。从url获取数据的问题目标C

{ 
    "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", 
             }, 

             ] 
        } 
        ] 
} 

只得到拉梅什的细节从上面的输出,剩下的数据在我的表视图不显示。以下是我的代码,我已经尝试从上述服务。请帮忙找出问题。 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]; 

    } 
} 
+0

最后一个回路是什么?你最终只能设置上次迭代的'temp'的值。 – n00bProgrammer

+0

您只在最后一个循环中添加最后一个对象。每次迭代都会覆盖它。您应该在循环内创建一个新对象,然后将其添加到数组中。 – Balanced

+0

您正在将Whole'EmpTable'数组添加为数组中的对象。所以数组中只有一个对象。这就是为什么只有一个单元格将被添加到tableView。尝试从EmpTable数组中提取数组对象。 –

您正在将Whole EmpTable数组添加为数组中的对象。所以数组中只有一个对象。这就是为什么在tableView上只能添加一个单元格。尝试从EmpTable数组中提取数组对象。

- (void)jsonData:(NSDictionary *)jsonDict方法

替换

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

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

cellForRowAtIndexPath

替换

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

} 

随着

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

希望这有助于。

+0

谢谢ellobarate。效果很好。 – Rajeev

_empArr.count永远是1,因为你只有一次 “EmpTable” 对象中。即使你解决了这个问题,然后在cellForRowAtIndexPathfor (NSDictionary *temp in profiledict)中循环遍历所有数组并永不停止,因此每次它将成为填充单元格字段的最后一个对象。

+0

我不明白。请问您可以通过代码@shebuka .TIA – Rajeev