我对以下服务有疑问。
{
"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];
}
}
答案 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"];
希望这有帮助。