0

i make an application that contain five view with segment controller my Views Name is NEWS,INTERVIEW,REVIEW,GALLERY etc.Here NEWS and INTERVIEW View Controller contain tableview with JSON Data Parsing when my application run then First load NEWSView when i first scroll it then it load more data but when segment InterviewViewController and First scroll it and then scroll a NEWS Table then it not load a more data please give me solution my Code is

- (void)viewDidLoad
 {
pageNum=0;
self.imageArray =[[NSMutableArray alloc]init];
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];
});
[self.spinner startAnimating];

}
-(void)fetchedData:(NSData *)responsedata
{
NSError* error;
self.json = [NSJSONSerialization JSONObjectWithData:responsedata options:kNilOptions error:&error];
if ([[_json objectForKey:@"data"] isKindOfClass:[NSArray class]])
{
    NSArray *arr = (NSArray *)[_json objectForKey:@"data"];
    [self.imageArray addObjectsFromArray:arr];
    [self.newsTable reloadData];
    NSLog(@"images,%@",self.imageArray);
}
[self.spinner stopAnimating];
self.spinner.hidesWhenStopped=YES;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.imageArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (self.imageArray == nil || self.imageArray.count <1)
{
    return 0;
}
else
{
    return 1;
}
}
-(CustumCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *Cellidentifier=@"Cell";
CustumCell *cell=[tableView dequeueReusableCellWithIdentifier:Cellidentifier];
if (cell ==nil)
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustumCell" owner:self options:nil];
    cell = [nib objectAtIndex:0];
}
{
    NSDictionary *dict = [self.imageArray objectAtIndex:indexPath.section];
    NSString *img2=[dict valueForKey:@"post_image"];
    [cell.imagePhoto sd_setImageWithURL:[NSURL URLWithString:img2] placeholderImage:[UIImage imageNamed:@"Hisoka.jpg"]];


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

    NSString *contents=[dict valueForKey:@"post_content"];
    if([contents isKindOfClass:[NSString class]])
    {
        cell.descriptionLabel.text = contents;
    } else
    {
        cell.descriptionLabel.text =  @"";
    }
    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.imageArray objectAtIndex:indexPath.section];
NSString *url=[dictionary valueForKey:@"link"];
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:url]];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}
Ashish Gabani
  • 445
  • 1
  • 5
  • 30
  • there is no issue in this implementation. – Danial Hussain Oct 27 '14 at 05:40
  • you just need to sort out that your count of items in array that is the source for your tableview. For that you just need to do NSLog. – Danial Hussain Oct 27 '14 at 05:41
  • @DanialHussain i check it but it was print only one page data when i scroll it it was not any thing print – Ashish Gabani Oct 27 '14 at 05:47
  • -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.imageArray.count; } log in this function – Danial Hussain Oct 27 '14 at 05:48
  • it can be that you are parsing wrong keys or the data may be array or something and got exception – Danial Hussain Oct 27 '14 at 05:49
  • i send you my project please give me email address. – Ashish Gabani Oct 27 '14 at 05:52
  • Your implementation is wrong. `scrollViewDidScroll` method gets called everytime you scroll the TableView several times, it might be getting incremented beyond the limit, and the api is not returning any data. Best way to do this is to have total number of pages in your api, and determine next page when the TableView scrolls to the bottom of see this http://stackoverflow.com/a/17860149/790842, then only increase the rowcount upto total. – iphonic Oct 27 '14 at 05:53
  • i think you should call api for once and download and parse all data but show number of items as required. – Danial Hussain Oct 27 '14 at 06:00
  • @iphonic bro when i first Scroll NEWS Table view then it load data but when i first Scroll another view Table and back to the News Table and scroll it it was not working. – Ashish Gabani Oct 27 '14 at 06:01
  • Put `NSLog(@"%d",pageNum);` in `scrollViewDidScroll`. I think its the culprit. – iphonic Oct 27 '14 at 06:04
  • it print 1,2 then direct 9 @iphonic – Ashish Gabani Oct 27 '14 at 06:12

1 Answers1

0

Add this scrollView Delegate method. Inside it add you code:

Note: Do not give multiple request with getData method .(After get the request result after that only give other result- do it in your own idea)

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
  NSInteger currentOffset = scrollView.contentOffset.y;
    NSInteger maximumOffset = scrollView.contentSize.height- scrollView.frame.size.height;
    if (maximumOffset - currentOffset <= 0) {

     // do your code here.
     [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];
});
}
Vineesh TP
  • 7,755
  • 12
  • 66
  • 130