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

时间:2013-10-25 00:42:19

标签: objective-c uitableview uiscrollview thrift uiscrollviewdelegate

我有Thrift服务器,我可以获取信息并在我的UITableView中加载它们,第一次加载10行,每次滚动到最后我想再加载10行(有信息)但是它永远不会工作对我来说,

请你帮我解决这个问题,

提前致谢!

这是我的numberOfRowsInSection方法

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

return _notes.count;
}

我的方法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

}

以下是我连接服务器的方式

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

3 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:2)

你应该提出你的要求。正在viewdid上执行的那个...从viewdidappear中删除将其放入方法中。然后从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];
     }
}

答案 2 :(得分:2)

嗨使用它对我来说非常适合尝试这个...

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