Reputation: 177
i want to make a application that Shows JSON data in UITableView in iOS.Here my webservices contain 3 to 4 page.So,i want when table view scrolled load next page data. then i code for it
- (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;
}
then Problem is that when i scroll table view it shows next page data but First page data was removed i want to keep all page data in to table view so please give me solution I know this question was asked in past but it was not working for me please so give me solution.
Upvotes: 0
Views: 2225
Reputation: 337
The problem is that you are replacing your old page with the new page you get,so you only need to append the new data to the the old array data.
If you have allocated self.dataArray
before using it in fetchedData
then just use
NSArray* newArray=[_json objectForKey:@"data"];
if(newArray && [newArray isKindOfClass:[NSArray class]])
[self.dataArray addObjectsFromArray:newArray];
else you need to allocate the array for the first page you get, and append the next pages data to it later.
NSArray* newArray=[_json objectForKey:@"data"];
if(newArray && [newArray isKindOfClass:[NSArray class]]){
if (!self.dataArray)
self.dataArray=[NSMutableArray arrayWithArray:newArray];
else
[self.dataArray addObjectsFromArray:newArray];
}
Upvotes: 1
Reputation: 803
The problem is that you override self.dataArray
in fetchedData:
method.
So, you need to add objects to array. I suppose you are using NSMutableArray
.
[self.dataArray addObject:[_json objectForKey:@"data"]];
BTW, small code improvements:
1) you can use method getData
in viewDidLoad
instead of copy/paste the code twice.
2) if you do performSelectorOnMainThread:@selector(fetchedData:)
then you dont need to have dispatch_async(dispatch_get_main_queue()
because it is already in main thread.
Upvotes: 0