2016-12-17 91 views
0

当我滚动到我的tableview的底部,并获取更多的数据与下一个偏移量,我将新的对象添加到我的数据数组。然而,我的tableview重新加载并返回到第一个单元格。我试图阻止这一点。Tableview在添加新数据时滚动到顶部。无限滚动

enter image description here

enter image description here

- (void)fetchMovies { 
    [MBProgressHUD showHUDAddedTo:self.view animated:YES]; 

    [self.networkingHelper fetchNowPlayingWithCompletionHandler:^(NSArray *objects, NSError *error) { 

    if (error) { 
     [self showErrorView:self.errorView]; 
    } else { 
     [self hideErrorView:self.errorView]; 
    } 

    [self.movies addObjectsFromArray:objects]; 
    self.displayedItems = self.movies; 

    dispatch_async(dispatch_get_main_queue(), ^{ 
     self.isMoreDataLoading = false; 

     if ([[NSThread currentThread] isMainThread]){ 
      NSLog(@"In main thread--completion handler"); 

      [self.refreshControl endRefreshing]; 
      [self.loadingMoreView stopAnimating]; 
      [MBProgressHUD hideHUDForView:self.view animated:YES]; 
     } else { 
      NSLog(@"Not in main thread--completion handler"); 
     } 
     }); 
     } 

    ]; 
} 

编辑尝试添加该

- (void)fetchMovies { 
    [MBProgressHUD showHUDAddedTo:self.view animated:YES]; 

    [self.networkingHelper fetchNowPlayingWithCompletionHandler:^(NSArray *objects, NSError *error) 
    { 

     if (error) 
     { 
      [self showErrorView:self.errorView]; 
     } 
     else 
     { 
      [self hideErrorView:self.errorView]; 

     } 
     [self.movies addObjectsFromArray:objects]; 
     self.displayedItems = self.movies; 


     dispatch_async(dispatch_get_main_queue(), ^{ 
      self.isMoreDataLoading = false; 


      if ([[NSThread currentThread] isMainThread]){ 
       NSLog(@"In main thread--completion handler"); 

       CGFloat oldOffset = self.moviesTableView.contentSize.height; 
       [self.moviesTableView reloadData]; 
       CGFloat onewOffset = self.moviesTableView.contentSize.height; 
       CGPoint new = CGPointMake(0, onewOffset - oldOffset); 

       [self.moviesTableView setContentOffset:new]; 

       [self.refreshControl endRefreshing]; 
       [self.loadingMoreView stopAnimating]; 
       [MBProgressHUD hideHUDForView:self.view animated:YES]; 


      } 
      else{ 
       NSLog(@"Not in main thread--completion handler"); 
      } 

      }); 
    } 

    ]; 
} 

还是滚动到顶部

+0

我不认为我很清楚。我的意思是我的tableview自动进入顶层单元格。我试图阻止这一点。所以用户可以继续滚动浏览下一页的新条目 – mparrish91

+0

,那么你想滚动底部意味着你的最后一个单元? –

+0

是对最后一个单元格。当用户在桌面上向下滑动并请求下一页时。有桌面停留并显示下一页。 – mparrish91

回答

1

要设置

[self.moviesTableView setContentOffset:CGPointMake(0, self.searchController.searchBar.frame.size.height)]; 

addSearchBar方法正在被更多的负荷结束后调用,设置的内容在初始点偏移。

也管理你setupInfiniteScrollView你在子视图中添加Tableview的地方。

并且存在与逻辑有点变化

取这个老y偏移位置

CGFloat oldOffset = self.moviesTableView.contentOffset.y; 

设置相同的Y位置负荷后更像

CGPoint new = CGPointMake(0, oldOffset); 
[self.moviesTableView setContentOffset:new]; 
+0

我没有得到你 –

+0

谢谢你这个修复了我。在其他地方设置内容偏移的愚蠢错误。我没有注意到didLayoutSubviews在表格滚动中被多次调用。在layoutSubviews中,我使用了addSearchBar函数。在它中,我在初始点抵消了我的搜索栏。 – mparrish91

+0

乐意帮忙... –

0

行添加在您的

if ([[NSThread currentThread] isMainThread]){ 
     NSLog(@"In main thread--completion handler"); 

     [self.refreshControl endRefreshing]; 

     NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[YOURARRAY.count] - 1 inSection:0]; 
     [self.YOURTABLLENAME scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES]; 

     [self.loadingMoreView stopAnimating]; 
     [MBProgressHUD hideHUDForView:self.view animated:YES]; 
} 
0

您需要在收到您的数据后,使用行的动态插入:

NSMutableArray *arrayOfInsertedIndexes = [NSMutableArray array]; 
NSInteger currentCount = self.yourOldData.count; 
for (int i =0; i < yourNewData.count; i++) 
{ 
     [arrayOfInsertedIndexes addObject:[NSIndexPath indexPathForRow:currentCount+i inSection:0]]; 
} 
[self.tableView beginUpdates]; 
[self.tableView insertRowsAtIndexPaths:arrayOfInsertedIndexes withRowAnimation:UITableViewRowAnimationRight]; 
[self.tableView endUpdates]; 

不要使用

[self.tableView reloadData]; 
+1

您已使用arrayOfInsertedIndexes值更新您的答案。 –

+0

arrayofinsertedindexes只是新数据而不是整个数据数组? – mparrish91

+0

@ mparrish91看一看。 –

0

的问题是,经过reloadData contentSize不会改变同步。你应该在reloadData之后调用layoutIfNeeded。只有经过contentSize改变

试试这个

//..... 
if ([[NSThread currentThread] isMainThread]){ 
    NSLog(@"In main thread--completion handler"); 

    CGFloat oldOffset = self.moviesTableView.contentSize.height; 
    [self.moviesTableView reloadData]; 
    [self.moviesTableView layoutIfNeeded]; 
    CGFloat onewOffset = self.moviesTableView.contentSize.height; 
    CGPoint new = CGPointMake(0, onewOffset - oldOffset); 

    [self.moviesTableView setContentOffset:new]; 
    [self.refreshControl endRefreshing]; 
    [self.loadingMoreView stopAnimating]; 
    [MBProgressHUD hideHUDForView:self.view animated:YES]; 
//....... 
0

目标C:

NSIndexPath* ip = [NSIndexPath indexPathForRow:rowNumberHere inSection:sectionNumberHere]; 
[theTableView scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionTop animated:NO]; 

Swift:

let offset = CGPoint(x: 0, y: self.tableView.contentSize.height - self.tableView.frame.size.height + self.FOOTERHEIGHT); 
self.tableView.contentOffset = offset;