UITableView insert rows without scrolling

前端 未结 6 1381
我在风中等你
我在风中等你 2020-12-05 03:13

I have a list of data that I\'m pulling from a web service. I refresh the data and I want to insert the data in the table view above the current data, but I want to keep my

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-05 03:59

    I am using self sizing cells and the estimated row height was pretty useless because the cells can vary significantly in size. So calculating the contentOffset wasn't working for me.

    The solution that I ended up with was quite simple and works perfectly. So first up I should mention that I have some helper methods that allow me to get the data element for an index path, and the opposite - the index path for a data element.

    -(void) loadMoreElements:(UIRefreshControl *) refreshControl {
      NSIndexPath *topIndexPath = [NSIndexPath indexPathForRow:0 inSection:0]
      id topElement = [myModel elementAtIndexPath:topIndexPath];
    
      // Somewhere here you'll need to tell your model to get more data
      [self.tableView reloadData];
    
      NSIndexPath *indexPath = [myModel indexPathForElement:topElement];
    
      [self.tableView scrollToRowAtIndexPath:indexPath 
                            atScrollPosition:UITableViewScrollPositionTop 
                                    animated:NO];
    
      [refreshControl endRefreshing];
    }
    

提交回复
热议问题