使用iOS中的分页在Tableview中加载更多JSON数据

时间:2014-10-24 10:39:48

标签: ios objective-c json uitableview

我想创建一个在iOS中的UITableView中显示JSON数据的应用程序。这里我的webservices包含3到4页。所以,我希望当表视图滚动加载下一页数据时。然后我为它编码

- (void)viewDidLoad
{
pagenum=1;

NSURL * url=[NSURL URLWithString:[NSString stringWithFormat:@"http://www.truemanindiamagazine.com/webservice/news.php?page=%d",pagenum]];
[self.newsTable setShowsHorizontalScrollIndicator:NO];
[self.newsTable setShowsVerticalScrollIndicator:NO];
[super viewDidLoad];
dispatch_async(kBgQueue, ^{
    data = [NSData dataWithContentsOfURL: url];
    [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
});

}
-(void)fetchedData:(NSData *)responsedata
{
NSError* error;
self.json = [NSJSONSerialization JSONObjectWithData:responsedata options:kNilOptions error:&error];
self.dataArray=[_json objectForKey:@"data"];
if (self.dataArray.count > 0)
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.newsTable reloadData];
    });
}
NSLog(@"images,%@",self.dataArray);
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.dataArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
return 1;
 }
 -(TableCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
static NSString *Cellidentifier=@"Cell";
TableCell *cell=[tableView dequeueReusableCellWithIdentifier:Cellidentifier];
if (cell ==nil)
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TableCell" owner:self options:nil];
    cell = [nib objectAtIndex:0];
}
{
NSDictionary *dict = [self.dataArray objectAtIndex:indexPath.section];
NSString *img2=[dict valueForKey:@"post_image"];
[cell.newsImage sd_setImageWithURL:[NSURL URLWithString:img2] placeholderImage:[UIImage imageNamed:@"Hisoka.jpg"]];


NSString *title=[dict valueForKey:@"post_title"];
cell.headLabel.text=title;

NSString *content=[dict valueForKey:@"post_content"];
cell.descripLabel.text=content;

NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSString *date=[dict valueForKey:@"post_date"];
NSDate * dateNotFormatted = [dateFormatter dateFromString:date];
[dateFormatter setDateFormat:@"d-MMM-YYYY"];
NSString * dateFormatted = [dateFormatter stringFromDate:dateNotFormatted];
cell.dateLabel.text=dateFormatted;
}
return cell;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView

{
pagenum=pagenum+1;
[self getData];
}

 -(void)getData {
NSURL * url=[NSURL URLWithString:[NSString stringWithFormat:@"http://www.truemanindiamagazine.com/webservice/news.php?page=%d",pagenum]];
dispatch_async(kBgQueue, ^{
    data = [NSData dataWithContentsOfURL:url];
    [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
});
 }
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *dictionary=[self.dataArray objectAtIndex:indexPath.section];
NSString *url=[dictionary valueForKey:@"link"];
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:url]];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}

然后问题是,当我滚动表视图时,它显示下一页数据但第一页数据被删除我想将所有页面数据保存到表视图中所以请给我解决方案 我知道过去曾问过这个问题,但这对我不起作用,请给我解决方案。

2 个答案:

答案 0 :(得分:1)

问题在于您使用新获得的页面替换旧页面,因此您只需将新数据附加到旧数组数据中。

如果您在self.dataArray中使用之前已分配fetchedData,请使用

NSArray* newArray=[_json objectForKey:@"data"];
if(newArray && [newArray isKindOfClass:[NSArray class]])
  [self.dataArray addObjectsFromArray:newArray];

否则你需要为你得到的第一页分配数组,并在以后将下一页数据附加到它。

NSArray* newArray=[_json objectForKey:@"data"];
if(newArray && [newArray isKindOfClass:[NSArray class]]){
  if (!self.dataArray)
     self.dataArray=[NSMutableArray arrayWithArray:newArray];
  else
     [self.dataArray addObjectsFromArray:newArray];
}

答案 1 :(得分:0)

问题是您在self.dataArray方法中覆盖fetchedData:

因此,您需要将对象添加到数组中。我想你正在使用NSMutableArray

[self.dataArray addObject:[_json objectForKey:@"data"]];

BTW,小代码改进:

1)您可以在getData中使用方法viewDidLoad,而不是将代码复制/粘贴两次。

2)如果你performSelectorOnMainThread:@selector(fetchedData:),那么你不需要dispatch_async(dispatch_get_main_queue(),因为它已经在主线程中。