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

标签 ios objective-c json uitableview

我想制作一个在 iOS 的 UITableView 中显示 JSON 数据的应用程序。这里我的 Web 服务包含 3 到 4 页。所以,我想在表格 View 滚动时加载下一页数据。然后我为它编码

- (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;
}

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

最佳答案

问题是你正在用你得到的新页面替换你的旧页面,所以你只需要将新数据附加到旧数组数据。

如果你在 fetchedData 中使用它之前分配了 self.dataArray 那么就使用

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];
}

关于ios - 在 iOS 中使用分页在 Tableview 中加载更多 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26546322/

相关文章:

ios - 我们可以使用安装在 VMWare 上的 MacOS 将应用程序发布到 AppStore (iOS) 吗?

objective-c - 以实例创建者作为接收者调用方法

objective-c - 使 NSView 不在其边界之外剪辑 subview

json - 无法将类型 '__NSCFBoolean' (0x1b5905868) 的值转换为 'NSDecimalNumber' (0x1b5912c70)

json - REST API JSON 错误代码和错误消息

ios - Cocos2d : How to check Sprite Frame Name is available or not?

ios - UITextView 滚动不起作用

c# - TabbedPage 无法在使用 MAUI 的 iOS 上工作,而是显示空白页面

ios - 在状态栏下显示tableView

json - 如何使用 redactor.js 插入单引号?