在uitableview中调用scrollviewdidscroll方法?

Dal*_*eet 0 json objective-c uitableview ios afnetworking

我正在构建一篇文章阅读应用程序.我正在使用AFNetworking第三方库将JSON数据提取到UITableView中.

假设Json链接是www.example.com&page = 1给出1-10篇文章,www.example.com&page = 2给出11-20篇文章等等.

我已经实现了分页和scrollViewDidScroll方法意味着当用户滚动它时会给出下一篇文章.

我正面临一个问题,当应用程序启动和UITableView加载scrollViewDidScroll方法调用三次但预期调用一次.

我在scrollViewDidScroll方法中使用增量变量进行分页,因为我说它调用三次,x值变为3并给出30篇文章.

当用户再次滚动时,它会给出接下来的30篇文章.我无法弄清楚为什么当启动应用程序时,scrollViewDidScroll方法会调用三次.

这是我的代码:

        - (void)viewDidLoad
            {
              [super viewDidLoad];
              tempJson = [[NSMutableArray alloc] init];
                [self loadNinjas];
             }

           - (void)loadNinjas {

          NSString *jsonLink=[NSString stringWithFormat:@"www.example.com&page=%d",x];
          NSURL *url = [[NSURL alloc] initWithString:jsonLink];
          NSURLRequest *request = [NSURLRequest requestWithURL:url];
          AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    operation.responseSerializer = [AFJSONResponseSerializer serializer];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSArray *jsonArray = (NSArray *)responseObject;
        for (NSDictionary *dic in jsonArray) {
        Json *json = [[Json alloc] initWithDictionary:dic];
        [tempJson addObject:json];
        }
          self.jsons = [[NSArray alloc] initWithArray:tempJson];
          [self.tableView reloadData];
          } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
           UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                message:[error localizedDescription]
                                               delegate:nil
                                      cancelButtonTitle:@"Ok"
                                      otherButtonTitles:nil];
           [alertView show];
           }];
         [operation start];
        }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
       {
       return self.jsons.count ;

        }

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
         {
           static NSString *Cellidentifier1 = @"ysTableViewCell";

           ysTableViewCell *cell1 = [tableView 

           dequeueReusableCellWithIdentifier:Cellidentifier1 forIndexPath:indexPath];
           cell1.TitleLabel1.text = [self.jsons[indexPath.row] title];

           cell1.AuthorLabel1.text = [self.jsons[indexPath.row] author];
          [cell1.ThumbImage1 setImageWithURL:[NSURL URLWithString:

           [self.jsons[indexPath.row] a_image]]];
           return cell1;}
         - (void)scrollViewDidScroll: (UIScrollView*)scroll {

            CGFloat currentOffset = scroll.contentOffset.y;
            CGFloat maximumOffset = scroll.contentSize.height -       scroll.frame.size.height;

            self.tableView.contentInset = UIEdgeInsetsMake(65, 0, 0, 0);
            if (maximumOffset - currentOffset <= -60.0) {
             x++;

            [self loadNinjas];
            [self.tableView addInfiniteScrollingWithActionHandler:^{
             }];
          [self.tableView reloadData];
         }
      }
Run Code Online (Sandbox Code Playgroud)

Mer*_*Dan 5

- (void)scrollViewDidScroll: (UIScrollView*)scroll 在滚动时被称为一杯次

你应该更好地使用:

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
Run Code Online (Sandbox Code Playgroud)

要么

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView
   willDecelerate:(BOOL)decelerate
Run Code Online (Sandbox Code Playgroud)