0

我有 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];
}
4

3 回答 3

3

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

[tableView addInfiniteScrollingWithActionHandler:^{
    // prepend data to dataSource, insert cells at top of table view
    // call [tableView.pullToRefreshView stopAnimating] when done
}];
于 2014-01-26T06:58:59.360 回答
2

你应该提出你的要求。在 viewdidappear 上执行的那个..从 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];
     }
}
于 2013-10-25T02:50:21.767 回答
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...        
    }
    } 
}
于 2013-10-25T04:54:35.210 回答