【发布时间】: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