如何在表或集合视图中实现无限加载?

问题描述:

我建立的文章阅读应用。我从JSON链接中获取数据,如文章图像和标题在uitableview中。如何在表或集合视图中实现无限加载?

我无法在uitableview中实现分页,比如说我的JSON链接是www.example.com & page = 1包含10个文章,每次只有1-10个。

当我像www.example.com的JSON链接串连&页= 2拿到11-20文章列表。

我无法实现我如何在UITableView中加载上滚动的数据,并增加节数与数据行。

这里是我的代码:

 int *x=1; 
    int *inc=10; 

    @interface ysTableViewController() 

    { 
    Reachability *internetReachable; 
    } 
    @end 

     @implementation ysTableViewController 

    - (id)initWithStyle:(UITableViewStyle)style 
    { 
     self = [super initWithStyle:style]; 
     if (self) { 
    // Custom initialization 
     } 
     return self; 
    } 

    - (void)viewDidLoad 
    { 
     [super viewDidLoad]; 
     [self checkInternetConnection]; 
     UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20,10,0,20)]; 
     titleLabel.textColor = [UIColor blackColor]; 
     titleLabel.backgroundColor = [UIColor clearColor]; 
     titleLabel.text = @"Story"; 
    [self.navigationItem setTitleView:titleLabel]; 


     } 

    - (void)scrollViewDidScroll:(UIScrollView *)aScrollView { 

    CGPoint offset = aScrollView.contentOffset; 

    CGRect bounds = aScrollView.bounds; 

    CGSize size = aScrollView.contentSize; 

    UIEdgeInsets inset = aScrollView.contentInset; 

    float y = offset.y + bounds.size.height - inset.bottom; 

    float h = size.height; 

    float reload_distance = 10; 

    if(y > h + reload_distance) { 

     NSLog(@"load more rows"); 

     inc=inc+10; 

     BOOL myBool = [self isNetworkAvailable]; 

     if (myBool) 

    { 

     @try { 

      // for table cell seperator line color 

      self.tableView.separatorColor = [UIColor colorWithRed:190/255.0 green:190/255.0 blue:190/255.0 alpha:1.0]; 

      // for displaying the previous screen lable with back button in details view controller 

      UIBarButtonItem *backbutton1 = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleBordered target:nil action:nil]; 

      [[self navigationItem] setBackBarButtonItem:backbutton1]; 

      _Title1 = [[NSMutableArray alloc] init]; 

      _Author1 = [[NSMutableArray alloc] init]; 

      _Images1 = [[NSMutableArray alloc] init]; 

      _Details1 = [[NSMutableArray alloc] init]; 

      _link1 = [[NSMutableArray alloc] init]; 

      _Date1 = [[NSMutableArray alloc] init]; 

      NSString *urlString=[NSString stringWithFormat:@“www.example.com&page=%d",x]; 

      NSLog(@"xxxxx===%d",x); 

      NSURL *newUrl=[NSURL URLWithString:urlString]; 

      NSData* data = [NSData dataWithContentsOfURL:newUrl]; 

      NSArray *ys_avatars = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; 

      x++; 

      if(ys_avatars){ 

      for (int j=0;j<ys_avatars.count;j++) 

       { 

        [_Title1 addObject:ys_avatars[j][@"title"]]; 

        [_Author1 addObject: ys_avatars[j][@"author"]]; 

        [_Images1 addObject: ys_avatars[j][@"featured_img"]]; 

        [_Details1 addObject:ys_avatars[j][@"content"]]; 

        [_link1 addObject:ys_avatars[j][@"permalink"]]; 

        NSString *newStr=[ys_avatars[j][@"date"] substringToIndex:[ys_avatars[j][@"date"] length]-3]; 

        [_Date1 addObject:newStr]; 

       }  } 

      else 

      { 

       NSLog(@"asd"); 

        } } 

      @catch (NSException *exception) { 

      }   

     } 

     } 

    } 

     - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 

     { 

// Return the number of sections. 

     return 1; 

    } 

     - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 

    { 

// Return the number of rows in the section. 

     return inc; 

     } 

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

     { 

     static NSString *Cellidentifier1 = @"ysTableViewCell"; 

     ysTableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:Cellidentifier1 forIndexPath:indexPath]; 

      // Configure the cell... 

     long row = [indexPath row]; 

     cell1.TitleLabel1.text = _Title1[row]; 

     cell1.AuthorLabel1.text = _Author1[row]; 

      NSString *yourStoryUrl = [_Images1[indexPath.row] stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]; 

     if(yourStoryUrl) { 

      NSArray *subStringsUrl = [yourStoryUrl componentsSeparatedByString:@"/"]; 

     NSString *stripedName = [subStringsUrl lastObject]; 

     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

     NSString *documentsDirectory = [paths objectAtIndex:0]; 

    //Local stored image file path 

      NSString* filePath =[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",stripedName]]; 

      if(filePath) { 

      UIImage *image = [UIImage imageWithContentsOfFile:filePath]; 

      if(image) { 

      // Now the image will have been loaded and decoded and is ready to rock for the main thread 

       ysTableViewCell *updateCell =(id)[tableView cellForRowAtIndexPath:indexPath]; 

       if(updateCell) 

        updateCell.ThumbImage1.image=image; 

       cell1.ThumbImage1.image=image; 

      } else { 

      dispatch_queue_t taskQ = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 

      dispatch_async(taskQ, ^{ 

       NSURL *Imageurl = [NSURL URLWithString:yourStoryUrl]; 

       NSData *data = [NSData dataWithContentsOfURL:Imageurl]; 

       UIImage *images1 = [[UIImage alloc] initWithData:data]; 

       NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

       NSString *documentsDirectory = [paths objectAtIndex:0]; 

       NSData *imageData = UIImagePNGRepresentation(images1); 

       //_imagePath =[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",stripedName]]; 



       // NSLog((@"pre writing to file")); 

       if (![imageData writeToFile:filePath atomically:NO]) 

       { 

        NSLog((@"Failed to cache image data to disk")); 

       } 

       else 

       { 

        NSLog((@"the cachedImagedPath is %@",filePath)); 

       } 

       // Now the image will have been loaded and decoded and is ready to rock for the main thread 

       dispatch_sync(dispatch_get_main_queue(), ^{ 

        ysTableViewCell *updateCell =(id)[tableView cellForRowAtIndexPath:indexPath]; 

        if(updateCell) 

         updateCell.ThumbImage1.image=images1; 

        cell1.ThumbImage1.image=images1; 

       }); 

        }); 

     } 



    } else { 

     dispatch_queue_t taskQ = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 

     dispatch_async(taskQ, ^{ 

      NSURL *Imageurl = [NSURL URLWithString:yourStoryUrl]; 

      NSData *data = [NSData dataWithContentsOfURL:Imageurl]; 

      UIImage *images1 = [[UIImage alloc] initWithData:data]; 

      //  NSString *myString = [Imageurl absoluteString]; 

      //  NSLog(@"%@",myString); 

      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

      NSString *documentsDirectory = [paths objectAtIndex:0]; 



      NSData *imageData = UIImagePNGRepresentation(images1); 

      _imagePath =[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",stripedName]]; 



      // NSLog((@"pre writing to file")); 

      if (![imageData writeToFile:_imagePath atomically:NO]) 

      { 

       NSLog((@"Failed to cache image data to disk")); 

      } 

      else 

      { 

       // NSLog((@"the cachedImagedPath is %@",_imagePath)); 

      } 

     // Now the image will have been loaded and decoded and is ready to rock for the main thread 

       dispatch_sync(dispatch_get_main_queue(), ^{ 

       ysTableViewCell *updateCell =(id)[tableView   cellForRowAtIndexPath:indexPath]; 

       if(updateCell) 

        updateCell.ThumbImage1.image=images1; 

       cell1.ThumbImage1.image=images1; 

      }); 

      }); 

    } 

} 

return cell1; 

    } 
+1

注 - 您的代码太长。你绝对不得不把它分解成小块。每当你有一段约3行的代码时,把它作为一个单独的函数。它几乎是不可能的“读”和帮助您的代码 - 希望它可以帮助! – Fattie 2014-09-03 08:25:22

这绝非易事。总体而言,你需要的代码看起来像这样..

注意这一基本例行的四个非常不同的部分。

我从来没有找到工作“一般”的软件包这一问题的解决方案。

再次,看在这个“四节”:他们给你要找的逻辑!

-(void)forTerm:(NSString *)term doPageAfter:(int)doingThisPage 
    { 

    doingThisPage = doingThisPage + 1; 
    if (doingThisPage > 20) return; // never, ever, ever forget that!! heh. 

    [CLOUD search:term page:doingThisPage then:^(NSArray *thoseTenResults) 
    { 




    self.searchSpinner.hidden = YES; 

    // (step 1) IF IT IS "PAGE 1", we need to re-kick-off the array... 
    if (doingThisPage == 1) 
     CLOUD.searchResultsRA = [[NSMutableArray alloc] init]; 




    // (step 2) go ahead and add on these results 

    if (doingThisPage == 1) 
     { 
     [CLOUD.searchResultsRA addObjectsFromArray:thoseTenResults]; 
     [self.searchDisplay safelyReloadBouncyTable]; 
     } 
    else 
     { 
     [self.searchDisplay.collectionView performBatchUpdates:^ 
     { 
     NSUInteger oldSize = CLOUD.searchResultsRA.count; 
     [CLOUD.searchResultsRA addObjectsFromArray:thoseTenResults]; 
     NSUInteger newSize = CLOUD.searchResultsRA.count; 

     NSMutableArray *arrayWithIndexPaths = [NSMutableArray array]; 
     for (NSUInteger i = oldSize; i < newSize; i++) 
      [arrayWithIndexPaths 
       addObject:[NSIndexPath indexPathForRow:i inSection:0]]; 

     [self.searchDisplay justSignal]; 
     [self.searchDisplay.collectionView 
      insertItemsAtIndexPaths:arrayWithIndexPaths]; 
     } 
     completion:nil]; 
     } 




    // (step 3) indeed if it's the first page, do a drop-in for fun 
    if (doingThisPage == 1) [self.searchDisplay.view dropIn:nil]; 
    // (for a "new search" which is now being displayed, in your UX 
    // there would be some sort of indication of that fact - do it here) 




    // (step 4) IF there WERE results .. try another page! 
    if (thoseTenResults.count > 0) 
     [self forTerm:term doPageAfter:doingThisPage]; 
     // note we are calling this same routine, again!!! 
    } 
    ]; 
    } 
+0

@ Joe谢谢,能否请你解释一下这是为了什么:[CLOUD search:term page:doingThisPage then:^(NSArray * thoseTenResults) – Daljeet 2014-09-01 11:07:02

+0

你好,乔,请给我发送你找到的文章url ...谢谢! – Daljeet 2014-09-02 20:07:37

+0

你好Joe Blow,是的,我知道iOS中的UIWebView。事实上,我们使用UIWebView来显示文章。 – Daljeet 2014-09-03 08:54:01