0

I have Thrift server, I can get information and load them in my UITableView, at the first I load 10 rows, and after scrolling to the end each time I want to load 10 more rows(with information) but it's never work for me,

would you please help me in this implementation,

Thanks in advance!

Here is my numberOfRowsInSection method

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

return _notes.count;
}

my method scrollViewDidScroll

-(void)scrollViewDidScroll: (UIScrollView*)scrollView
{
float scrollViewHeight = scrollView.frame.size.height;
float scrollContentSizeHeight = scrollView.contentSize.height;
float scrollOffset = scrollView.contentOffset.y;

if (scrollOffset == 1)
{
    [self.tableView setContentOffset:CGPointMake(0, 44)];

}
else if (scrollOffset + scrollViewHeight == scrollContentSizeHeight)
{


  //  I don't know what should I put here
    NSLog(@"%@ we are at the end", _notes); //==> _notes is null 
  //we are at the end, it's in the log each time that scroll, is at the end

}

Here is how I connect to the server

  - (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:YES];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
                                         (unsigned long)NULL), ^(void) {

    NSURL *url = [NSURL URLWithString:BASE_URL];

    THTTPClient *transport = [[THTTPClient alloc] initWithURL:url];
    TBinaryProtocol *protocol = [[TBinaryProtocol alloc]
                                 initWithTransport:transport
                                 strictRead:YES
                                 strictWrite:YES];

    server = [[thrift_Client alloc] initWithProtocol:protocol];
    _notes = [server get_notes:10 offset:0 sort_by:0 search_for:result];


    __weak NotesTable *weakSelf = self;

    dispatch_async(dispatch_get_main_queue(), ^{
        [weakSelf.notesTable reloadData];
    });
});

[self.view setNeedsDisplay];
}
Ali Mohamad
  • 41
  • 2
  • 10

3 Answers3

3

Use SVPullToRefresh, it offers nice encapsulation. Add the library and register your UITableView with

[tableView addInfiniteScrollingWithActionHandler:^{
    // prepend data to dataSource, insert cells at top of table view
    // call [tableView.pullToRefreshView stopAnimating] when done
}];
liruqi
  • 1,524
  • 15
  • 12
2

You should put your request. The one that is executing on viewdidappear.. remove from viewdidappear put it inside a method. Then call this method from viewdidappear and from this place you put the log and instead of your implementation of didscroll this is one is better

- (void)scrollViewDidScroll: (UIScrollView *)scroll {
     NSInteger currentOffset = scroll.contentOffset.y;
     NSInteger maximumOffset = scroll.contentSize.height - scroll.frame.size.height;

     // Change 10.0 to adjust the distance from bottom
     if (maximumOffset - currentOffset <= 10.0) {
          [self methodThatAddsDataAndReloadsTableView];
     }
}
Lukas
  • 1,346
  • 7
  • 24
  • 49
Roberto Ferraz
  • 2,511
  • 24
  • 43
2

Hi use this it works perfectly for me try this...

- (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) 
    {

    //Put your load more data method here...        
    }
    } 
}
TamilKing
  • 1,643
  • 1
  • 12
  • 23