【问题标题】:array data from json duplicated on tableview ios来自json的数组数据在tableview ios上重复
【发布时间】:2016-11-10 23:53:38
【问题描述】:

我在我的 tableview 应用程序中显示来自 web 服务的数据,但我在 tableview 中获得了两次数组。 我也在尝试使用无休止的滚动来显示分页数据但没有成功

 (void)viewDidLoad {
[super viewDidLoad];

self.articlesArray = [[NSMutableArray alloc] init];
[self fetchData:1];

}

我的数据获取函数

-(void) fetchData:(int)page {
NSString *urlString = [NSString
                       stringWithFormat:@"http://url?page=%d", (int)page];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
NSData *theData = [NSURLConnection sendSynchronousRequest:request
                                        returningResponse:nil
                                                    error:nil];

self.articlesArray = [NSJSONSerialization JSONObjectWithData:theData
                                                     options:NSJSONReadingMutableContainers
                                                       error:nil];

[self.tableView registerNib:[UINib nibWithNibName:@"ArticleCell" bundle:nil] forCellReuseIdentifier:@"ArticleCell"];
}

这是我的 tableview 方法,请看看我做错了什么

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
 return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

if (section==0)
 {
 return [self.articlesArray count];
else
 return [self.articlesArray count];
}
//  return [self.articlesArray count]; 
}

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

NSInteger row = [indexPath row];
 if (3 == (row % 4)) {  // or 0 == if you  want the first cell to be an ad!
static NSString *MyIdentifier = @"AdCell";
AdViewCell  *cell = (AdViewCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if ((cell == nil) || (![cell isKindOfClass: AdViewCell.class]))
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"AdCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell = [[AdViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                               reuseIdentifier:MyIdentifier] ;
 }
   GADBannerView *bannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeMediumRectangle];

 bannerView.adUnitID =@"";
 bannerView.rootViewController =self;
 GADRequest *request = [GADRequest request];
 [bannerView loadRequest:request];

 [cell.contentView addSubview:bannerView];

 return cell;
}
 else{
static NSString *simpleTableIdentifier = @"ArticleCell";
ArticleViewCell *cell = (ArticleViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier ];
if ((cell == nil) || (![cell isKindOfClass: ArticleViewCell.class]))
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ArticleCell" owner:self options:nil];
cell = [nib objectAtIndex:1];
cell = [[ArticleViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                              reuseIdentifier:simpleTableIdentifier] ;
}
NSDictionary * tempDictionary = [self.articlesArray objectAtIndex:indexPath.row];
 NSString *imageUrl = [[self.articlesArray objectAtIndex:indexPath.row]objectForKey:@"featured_image"];
 imageUrl = [imageUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
 [cell.thumbnailImageView sd_setImageWithURL:[NSURL URLWithString:imageUrl ] placeholderImage:nil options:SDWebImageRetryFailed completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
if (image){
    // Set your image over here
}else{
    //something went wrong
    NSLog(@"Error occured : %@", [error description]);
}

}];
NSString * title=[tempDictionary valueForKeyPath:@"title.rendered"];

cell.titleLabel.text = title;

return cell;
}


}

【问题讨论】:

  • 请发布更多代码(numberOfItemsInSection、numberOfSections、cellForItemAt)
  • 您没有显示足够的代码来找出问题所在。一个建议是在 numberOfRowsInSection 上设置一个断点,并将其与您期望的行数进行比较。 registerNib 也应该在 viewDidLoad 中,因为它只需要设置一次。使用上面的代码,您每次 fetchData 时都会注册 nib,而且您应该只注册一次。
  • 顺便说一句,您还可以硬编码articlesArray中的数据并绕过调用fetchData来帮助隔离问题。
  • 请看更新
  • 交叉验证JSON数据是否重复?

标签: ios arrays json web-services uitableview


【解决方案1】:

一旦从 json 获得响应,您就可以删除重复值,在重新加载 tableview 之前从数组中删除重复元素

NSOrderedSet *orderedSet = [NSOrderedSet orderedSetWithArray:YourArray];

NSArray *arrayWithoutDuplicates = [orderedSet array];

谢谢

【讨论】:

  • 该数组有 10 个没有重复的对象我仔细检查了它的 tableview 显示它两次
  • 请检查里面的 numberOfRowsInSection ,这一定是一个问题,因为你传递了两次相同的数组
  • numberOfRowsInSection 与我的 web 服务按页返回 10 个对象完全相同
  • 如果您只想在第 0 部分显示一个单元格,然后保留到其他部分,然后尝试将下面的代码放入 numberOfRowsInSection if (section==0) { return 1; }else{ 返回 [self.articlesArray 计数]; }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-07
  • 2022-01-08
相关资源
最近更新 更多