从url Objective C获取数据的问题

时间:2017-06-14 12:15:51

标签: ios objective-c iphone xcode uitableview

我对以下服务有疑问。

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

                                        ]
                  }
                  ]
}

仅从上面的输出中获取Ramesh的详细信息,剩余数据不会显示在我的表视图中。以下是我从上述服务中尝试过的代码。请帮助找出问题所在。 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];

    }
}

2 个答案:

答案 0 :(得分:0)

_empArr.count将始终为1,因为您内部只有一个“EmpTable”对象。即使你修复了它,然后在cellForRowAtIndexPath的{​​{1}}中循环遍历所有数组并且永不停止,所以每次它都是填充单元格字段的最后一个对象。

答案 1 :(得分:0)

您正在将整个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"];

希望这有帮助。