0

我有Thrift服務器,我可以獲取信息並將它們加載到我的UITableView中,第一次加載10行,並且在每次滾動到結尾後每次我要加載10個行(與信息),但它從來沒有爲我提前上班,如何在滾動後在UITableView中加載更多數據

請你幫我在此實現,

謝謝!

這裏是我的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]; 
} 

回答

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]; 
    } 
} 
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...   
    } 
    } 
} 
3

使用SVPullToRefresh,它提供了很好的封裝。添加庫並註冊您的UITableView與

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