【发布时间】:2015-01-07 05:36:53
【问题描述】:
我已经完成了这里的问题,并尝试了其他人提出的一些建议,但它们似乎都不起作用。基本上,我从我的 web 服务接收到 JSON 数据,它正在填充一个数组,然后我希望将其放入 UITableViewCell 中。但我的问题是 tableview 单元格只显示我的数组中的一条记录。我需要以列表视图的形式向用户显示我的所有记录。
这是我的 Web 服务解析的 JSON 数组:
NSData *data = [soapResultsString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSArray *planArray = [json objectForKey:@"plan_type"];
NSLog(@"planArray==>%@",planArray);
NSLog(@"planArrayCount==>%d",[planArray count]);
for (int i = 0 ; i<[planArray count]; i++) {
NSLog(@"print==>%d",i);
recordResults = NO;
appDelegate.nameString = [[[json valueForKey:@"plan_type"]valueForKey:@"name"]objectAtIndex:i];
appDelegate.ageString=[[[json valueForKey:@"plan_type"]valueForKey:@"age"]objectAtIndex:i];
appDelegate.sexString=[[[json valueForKey:@"plan_type"]valueForKey:@"sex"]objectAtIndex:i];
appDelegate.deptString=[[[json valueForKey:@"plan_type"]valueForKey:@"dept"]objectAtIndex:i];
NSLog(@"name==%@,age==%@,sex==%@,dept==%@",appDelegate.nameString,appDelegate.ageString,appDelegate.sexString,appDelegate.deptString);
}
现在是 tableview 部分。我有一个视图控制器,我从插座放置了 Uitableview,然后我从自定义 UITableViewCell 合并了 UITableViewCell。 这是我的表格视图单元格编码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
mobilePlanDetailsCellTableViewCell *mobPlan = (mobilePlanDetailsCellTableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (mobPlan == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"mobilePlanDetailsCellTableViewCell" owner:self options:nil];
mobPlan = [nib objectAtIndex:0];
}
mobPlan.nameLabel.text = appDelegate.nameString;
mobPlan.ageLabel.text =appDelegate.ageString;
mobPlan.sexLabel.text =appDelegate.sexString;
mobPlan.deptLabel.text =appDelegate.sexString;
return mobPlan;
}
那么,你能告诉我如何将我的 Web 服务 JSON 中的所有数据加载到 UITableViewCell 中吗?
这是我的 NSLog 输出:
2015-01-07 10:41:00.145 AmslideMenu[430:5779] planArrayCount==>68
2015-01-07 10:41:00.145 AmslideMenu[430:5779] print==>0
2015-01-07 10:41:00.146 AmslideMenu[430:5779]name==james,age==17,sex==male,dept==civil
2015-01-07 10:41:00.146 AmslideMenu[430:5779] print==>1
2015-01-07 10:41:00.146 AmslideMenu[430:5779]name==rahul,age==17,sex==male ,dept==IT
2015-01-07 10:41:00.146 AmslideMenu[430:5779]print==>2
2015-01-07 10:41:00.146 AmslideMenu[430:5779]name==ramesh,age==18,sex==male,dept==Mechanic
【问题讨论】:
-
检查 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
-
[json valueForKey:@"plan_type"]是array还是dictionary? -
您将其分配给了一个数组。在
for循环中,你调用valueForKey也是一样的。 -
另外,您没有单独保存值。您只是在更改相同属性的值。那么如何获取所有数据呢?
-
json 是字典,planArray 是数组
标签: ios objective-c json uitableview