【问题标题】:Call of scrollviewdidscroll method in uitableview?在uitableview中调用scrollviewdidscroll方法?
【发布时间】:2014-09-18 11:23:24
【问题描述】:

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

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

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

当应用启动和 UITableView 加载 scrollViewDidScroll 方法调用了 3 次但预期调用了一次时,我遇到了问题。

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

当用户再次滚动时,它会给出接下来的 30 篇文章。我无法弄清楚为什么在启动应用程序时会调用 3 次 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];
         }
      }

【问题讨论】:

    标签: ios objective-c json uitableview afnetworking


    【解决方案1】:

    - (void)scrollViewDidScroll: (UIScrollView*)scroll 滚动时被调用多次

    你最好使用:

    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
    

    - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView
       willDecelerate:(BOOL)decelerate
    

    【讨论】:

    • 感谢它运行良好。 - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)减速
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多