从网址目标 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",
},
]
}
]
}

仅从上面的输出中获取 Ramesh 的详细信息,剩余数据不会显示在我的表视图中。以下是我从上述服务中尝试的代码。请帮助找出问题。蒂亚

- (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];
}
}

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

希望这有帮助。

_empArr.count将始终为 1,因为您里面只有一个 "EmpTable" 对象。即使你修复了这个问题,那么在cellForRowAtIndexPathfor (NSDictionary *temp in profiledict)中,你循环遍历所有数组并且永远不会停止,所以每次它都会是最后一个填充单元格字段的对象。

最新更新