如何在滚动后在UITableView中加载更多数据



>我有Thrift服务器,我可以获取信息并将其加载到我的UITableView中,首先我加载10行,每次滚动到最后我想再加载10行(带有信息),但它对我不起作用,

你能帮我实现这个吗,

提前感谢!

这是我的行数InSection方法。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _notes.count;
}

我的方法滚动视图确实滚动

-(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
}

这是我连接到服务器的方式

  - (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];
}

使用 SVPullToRefresh,它提供了很好的封装。添加库并注册您的 UITableView

[tableView addInfiniteScrollingWithActionHandler:^{
    // prepend data to dataSource, insert cells at top of table view
    // call [tableView.pullToRefreshView stopAnimating] when done
}];
嗨,

使用它非常适合我试试这个......

- (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...        
    }
    } 
}

你应该提出你的请求。正在查看时执行的那个出现。.从视图中删除它出现在一个方法中。然后从viewdidappear调用此方法,并从此处放置日志,而不是didscroll的实现,这是一个更好的方法

- (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];
     }
}

最新更新