【问题标题】:How to load all records from JSON array into UITableViewCell?如何将 JSON 数组中的所有记录加载到 UITableViewCell 中?
【发布时间】: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


【解决方案1】:

先创建一个属性来保存数据,

@property (nonatomic, strong) NSArray *dataArray;

创建一个NSObject 子类来表示一组属性,例如姓名、年龄等。

@interface User : NSObject

// Properties
@property (nonatomic, strong) NSString *name;
@property (nonatomic, assign) NSInteger age;

@end

然后在for循环中创建User对象并添加到dataArray

for (int i = 0 ; i<[planArray count]; i++) {

    recordResults = NO;
    User *user = [User alloc] init];
    user.nameString = [[[json valueForKey:@"plan_type"]valueForKey:@"name"]objectAtIndex:i];
    user.ageString=[[[json valueForKey:@"plan_type"]valueForKey:@"age"]objectAtIndex:i];
    user.sexString=[[[json valueForKey:@"plan_type"]valueForKey:@"sex"]objectAtIndex:i];
    user.deptString=[[[json valueForKey:@"plan_type"]valueForKey:@"dept"]objectAtIndex:i];

    [_dataArray addObject:user];
}

然后访问该数组以填充 tableView。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{ 静态 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];
}

User *user = [_dataArray objectAtIndex:indexPath.row];
mobPlan.nameLabel.text = user.nameString;
mobPlan.ageLabel.text = user.ageString;
mobPlan.sexLabel.text = user.sexString;
mobPlan.deptLabel.text = user.sexString;

return mobPlan;
}

【讨论】:

  • 妈妈我也试过那个方法,值越来越正确但是当我在 UItableview 单元格中填充时卡住只得到一个记录就是问题
  • @Anand- 您正在访问同一个 appDelegate 属性。如何获取所有数据?
  • okie 现在我知道了,让我试试你的答案,如果有任何问题,我会回复你,感谢你抽出宝贵时间,上帝保佑
【解决方案2】:

我认为这是因为您不了解如何存储值和 tableview 的数据源。

也许你的数据源和表格视图是分开的。

您应该将值存储到一个数组中,就像您现在所做的那样,您可以在 appDelegate 中声明一个数组。

@property NSArray *planTypes;

当你这样做时,你可以更改为

NSMutableArray *dataSource = [NSMutableArray arrayWithCapacity:planArray.count];
for (int i = 0 ; i<[planArray count]; i++) {
    NSLog(@"print==>%d",i);
    recordResults = NO;
    NSString *nameString = [[[json valueForKey:@"plan_type"]valueForKey:@"name"]objectAtIndex:i];
    NSString *ageString=[[[json valueForKey:@"plan_type"]valueForKey:@"age"]objectAtIndex:i];
    NSString *sexString=[[[json valueForKey:@"plan_type"]valueForKey:@"sex"]objectAtIndex:i];
    NSString *deptString=[[[json valueForKey:@"plan_type"]valueForKey:@"dept"]objectAtIndex:i];
    generally, we create n modelClass like STPerson {@property name, xxx...},and value the person
    NSDictionary *dictionary = @{@"name":nameString, @"age":ageString,...};
    [dataSource addObject:dictionary];
}
the most important comes:
// you store values into appDelegate
appDelegate.planTypes = dataSource;

存储值后,应该重写 UITableViewDataSource 中的一些方法

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return appDelegate.planTypes.count;
}

修改你的 cellForRow 方法:

- (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];
    }
    NSDictionary *plan = appDelegate.planTypes[indexPath.row];

    mobPlan.nameLabel.text = plan[@"name"];
    mobPlan.ageLabel.text = plan[@"age"];
    mobPlan.sexLabel.text = plan[@"sex"];
    mobPlan.deptLabel.text = plan[@"dept"];

    return mobPlan;
}

【讨论】:

  • 以上两种方法你应该都明白了。当你自定义你的 tableview 单元格时,你应该设置一个 newValue。如果要显示多个单元格,则应通过 tableView:numberOfRowsInSection: 方法告诉 tableview 它有多少个单元格
  • 我认为你的回答会对我有所帮助,首先我需要在 AppDelegate.h 中创建一个数组?
  • @Anand- 只需将数组声明为属性。这样做的目的是在不同对象之间共享值。共享值的方式有很多种,您可以这样做。
  • 我试过你的方法很好,但我的 tableview 单元格没有显示任何值
  • 你的 json 内容是什么
猜你喜欢
  • 1970-01-01
  • 2017-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多