ios – 仅在indexpath处调用一行的单元格

前端之家收集整理的这篇文章主要介绍了ios – 仅在indexpath处调用一行的单元格前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在解析一个json数据,如果它有一个内容,那么该字段之一是一个数组.但是如果它没有内容,那么它只是一个字符串“No result”.
我使用以下代码来解析它:
  1. -(void)viewWillAppear:(BOOL)animated
  2.  
  3.  
  4. {
  5. [super viewWillAppear:YES];
  6. //URL is called.
  7. if ([status isEqualToString:@"success"])
  8.  
  9. {
  10. if ([[[json objectForKey:@"Items"] objectForKey:@"item_list"] isKindOfClass:[NSString class]])
  11.  
  12.  
  13.  
  14. {
  15. _Label.text = [NSString stringWithFormat:@"0"];
  16. ItemsArray = [[NSMutableArray alloc]init];
  17. }
  18. else
  19. {
  20. ItemsArray = [[json objectForKey:@"Items"] objectForKey:@"item_list"];
  21. NSLog(@"%d",[ItemsArray count]);
  22. float Amount = 0;
  23. NSLog(@"%d",[ItemsArray count]);
  24. if ([ItemsArray count]>0)
  25. {
  26. for (int i=0; i<[ItemsArray count]; i++)
  27. {
  28. NSMutableDictionary * temp3 = [ItemsArray objectAtIndex: i];
  29. NSString * x = [temp3 objectForKey:@"final"];
  30. Amount = Amount + [x floatValue];
  31. }
  32. Label.text = [NSString stringWithFormat:@"%.2f",BidAmount];
  33. }
  34. else
  35. {
  36. Label.text = [NSString stringWithFormat:@"0"];
  37. }
  38. }
  39.  
  40.  
  41.  
  42. [TableViewOutlet reloadData];

}

这很好用.

当字段返回字符串时我面临没问题.但是当字段是数组时,我被卡住了.
在第一次运行项目后,即使数组计数大于0,它也能正常工作.但是从第二次开始,也不会调用Cellforrowatindexpath方法….
这些是我的代码

  1. -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
  2. {
  3. return 1;
  4. }
  5.  
  6. -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  7. {
  8. NSLog(@"%d",[ItemsArray count]);
  9. return [ItemsArray count];
  10. }
  11.  
  12. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  13.  
  14. {
  15. static NSString *CellIdentifier = @"TableViewCell";
  16. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  17.  
  18. if (cell==nil)
  19. {
  20. cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  21. }
  22. cell.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.025];
  23. NSDictionary *temp = [ItemsArray objectAtIndex:indexPath.row];
  24.  
  25. UILabel *ItemName = (UILabel *)[cell viewWithTag:1000];
  26. ItemName.text = [temp objectForKey:@"title"];
  27.  
  28. UILabel *Cost = (UILabel *)[cell viewWithTag:1001];
  29. Cost.text = [temp objectForKey:@"final"];
  30.  
  31. return cell;
  32.  
  33. }

有人请帮帮我

解决方法

我认为问题是你没有保留你的ItemArray.
  1. NSArray* array = [[json objectForKey:@"Items"] objectForKey:@"item_list"];
  2. ItemsArray = [NSMutableArray alloc]]initWithArray:array];

使用此代码片段.希望它可以工作.

猜你在找的iOS相关文章