2017-03-17 81 views
0

如何在滾動時重新加載表中的數據?在使用API​​滾動時在tableview中重新加載數據

如果我有一個url「http:....... & start = 0 & limit = 50」。

如何使用遞增與50

開始和限值

任何人都可以找到一個解決方案在重新加載表中的數據?

+0

我希望你要整合分頁! – kb920

+0

如何添加分頁?我沒有使用這種方法。 – Justin

+0

檢查此博客http://uiroshan.github.io/2015/02/01/ios-uitableview-load-data-while-scrolling/ – kb920

回答

0
  1. 您需要在this.In爲了做到這一點的表視圖來實現負載更多的功能,你需要跟蹤表視圖的索引,而它一旦滾動表到達最後一個單元格,你可以調用api,增加頁碼,並在獲得api的積極響應後將新記錄添加到數組中。

  2. 請參閱下面的代碼以獲得更多的理解。

  3. 變量初始化

  4. 在viewDidLoad方法

    arrTableData = [[NSMutableArray alloc]init]; 
    currentPage=-1; 
    hasNextPage = YES; 
    
  5. 在的tableView numberOfRowsInSection方法

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    { 
        int count = (int)arrTableData.count; 
        if (hasNextPage) 
        { 
        count++; 
        } 
        return count; 
    } 
    
  6. 在的cellForRowAtIndexPath方法

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
        UITableViewCell *cell = nil; 
        if (indexPath.row<arrTableData.count) 
        { 
        //Load TableviewCell you intend to show 
        } 
        else 
        { 
         //Show loading cell 
         cell = [self loadingCell:indexPath]; 
         if (!isLoadingNextPage) 
         { 
         [self fetchData]; 
         } 
        } 
        return cell; 
    } 
    
  7. 加載單元代碼

    -(UITableViewCell *) loadingCell :(NSIndexPath *)indexPath 
    { 
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:nil]; 
        cell.backgroundColor = [UIColor clearColor]; 
    UIActivityIndicatorView *activityIndicatorView =[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite]; 
    activityIndicatorView.color = [UIColor blackColor]; 
    activityIndicatorView.center = CGPointMake(self.tblView.center.x, cell.center.y); 
    [cell.contentView addSubview:activityIndicatorView]; 
    [activityIndicatorView startAnimating]; 
    
    cell.userInteractionEnabled = NO; 
    return cell; 
    } 
    
  8. API調用執行

    -(void)fetchData 
    { 
        isLoadingNextPage = YES; 
        if (currentPage==-1) 
         currentPage=0; 
        //API Call 
        { 
    DLog(@"API Call Response = %@",response); 
    isLoadingNextPage = NO; 
    if (response == nil) 
    { 
        hasNextPage=NO; 
        return; 
    } 
    
    if (success) 
    { 
        if (hasNextPage) 
        { 
         currentPage++; 
        } 
        [arrTableData addObjectsFromArray:response]; 
    } 
    else 
    { 
        hasNextPage=NO; 
    } 
    //Reload tableview 
    }]; 
    } 
    

這個替代的解決方案是 使用SVPullToRefresh庫中整合它的無限滾動。 https://github.com/samvermette/SVPullToRefresh

0
- (void)scrollViewDidScroll:(UIScrollView *)scrollView 
{ 
    if(self.comptabl.contentOffset.y >= (self.comptabl.contentSize.height - self.comptabl.bounds.size.height)) 
    { 
     if(isPageRefreshing==NO){ 
      isPageRefreshing=YES; 
      [appDelegate showIndicator:nil view1:self.view]; 
      start=start+50; 

      [self makeRequest:start]; 
      [self.comptabl reloadData]; 
      NSLog(@"called %d",start); 
     } 



    } 
} 

我使用這種方法,但在所有的時間值增加50 ..how我可以限量值

相關問題