我正在解析一个json数据,如果它有一个内容,那么该字段之一是一个数组.但是如果它没有内容,那么它只是一个字符串“No result”.
我使用以下代码来解析它:
我使用以下代码来解析它:
- -(void)viewWillAppear:(BOOL)animated
- {
- [super viewWillAppear:YES];
- //URL is called.
- if ([status isEqualToString:@"success"])
- {
- if ([[[json objectForKey:@"Items"] objectForKey:@"item_list"] isKindOfClass:[NSString class]])
- {
- _Label.text = [NSString stringWithFormat:@"0"];
- ItemsArray = [[NSMutableArray alloc]init];
- }
- else
- {
- ItemsArray = [[json objectForKey:@"Items"] objectForKey:@"item_list"];
- NSLog(@"%d",[ItemsArray count]);
- float Amount = 0;
- NSLog(@"%d",[ItemsArray count]);
- if ([ItemsArray count]>0)
- {
- for (int i=0; i<[ItemsArray count]; i++)
- {
- NSMutableDictionary * temp3 = [ItemsArray objectAtIndex: i];
- NSString * x = [temp3 objectForKey:@"final"];
- Amount = Amount + [x floatValue];
- }
- Label.text = [NSString stringWithFormat:@"%.2f",BidAmount];
- }
- else
- {
- Label.text = [NSString stringWithFormat:@"0"];
- }
- }
- [TableViewOutlet reloadData];
}
这很好用.
当字段返回字符串时我面临没问题.但是当字段是数组时,我被卡住了.
在第一次运行项目后,即使数组计数大于0,它也能正常工作.但是从第二次开始,也不会调用Cellforrowatindexpath方法….
这些是我的代码:
- -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
- {
- return 1;
- }
- -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- NSLog(@"%d",[ItemsArray count]);
- return [ItemsArray count];
- }
- -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- static NSString *CellIdentifier = @"TableViewCell";
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
- if (cell==nil)
- {
- cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
- }
- cell.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.025];
- NSDictionary *temp = [ItemsArray objectAtIndex:indexPath.row];
- UILabel *ItemName = (UILabel *)[cell viewWithTag:1000];
- ItemName.text = [temp objectForKey:@"title"];
- UILabel *Cost = (UILabel *)[cell viewWithTag:1001];
- Cost.text = [temp objectForKey:@"final"];
- return cell;
- }
有人请帮帮我
解决方法
我认为问题是你没有保留你的ItemArray.
- NSArray* array = [[json objectForKey:@"Items"] objectForKey:@"item_list"];
- ItemsArray = [NSMutableArray alloc]]initWithArray:array];
使用此代码片段.希望它可以工作.