0

I have a segment control with a table view. When user clicks segment control, then table view changes data source. I would like table to scroll to last position user had seen.

For example: user was in segment 1 and looking at table view line 3.

segment 1 | segment 2

line 3
line 4
line 5

Then user clicked segment 2 then clicked back to segment 1. I want table view scroll to line 3.

Code is as below. In viewDidAppear I have same code to get lastSavedRow and call scrollToRowAtIndexPath. When app gets loaded the scrolling works. However, in segmentValueChanged action it doesn't roll to correct position. In debugger I am sure the lastSavedRow has correct number.

- (IBAction) segmentVauleChanged : (id)sender
{
     // remember row number before segment value changes

     NSArray *indexPaths = [self.tableView indexPathsForVisibleRows];
     NSArray * sortedIndexPaths = [indexPaths sortedArrayUsingSelector:@selector(compare:)];
     NSIndexPath *firstVisibleIndexPath = [sortedIndexPaths objectAtIndex:0];

     // function to write row number and seg index to user default
     [self saveScrollPositionInSegment: self.currentSegmentSelectedIndex row:firstVisibleIndexPath.row];  

     self.currentSegmentSelectedIndex = segmentController.selectedSegmentIndex;

     [self.tableView reloadData];

     // scroll to last saved position
     int lastSavedRow = [self getScrollPositionInSegment:segmentController.selectedSegmentIndex];

     [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:lastSavedRow inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES]; 

}

Does anyone have same problem? Many thanks in advance!

3 Answers3

0

The lastSavedRow will on the top of the current tableview. I think there maybe something wrong with the dealing with datasource. Maybe after change the datasource, the last postion the user seen was not the index you figured out! If you provide more information or your example, we can help you to find out the mistake.

Dennis
  • 455
  • 2
  • 7
  • 14
  • I added some details. The datasource is correctly displayed, and since scrolling is after tableView reloadData, I am not sure how it relates. And I checked in debug, the lastSavedRow is the number I expect. But whatever the number is, the screen always roll to top. If you need more details, please let me know. Thank you for helping! – user3775830 Jun 25 '14 at 17:40
  • I think @hackerinheels is right, do as he suggested, if it doesn't work,maybe you can share a demo for us. – Dennis Jun 26 '14 at 03:29
0

I would do the following.

1 .Call visibleCells before calling indexPathsForVisibleRows

[self.tableView visibleCells];

2.Look at the need for sorting the array before identifying firstVisibleIndexPath

3.reloadData is not synchronous so its possible that the table is not completed loading before you try to scroll to an index. So try to see if you can do the scrolling in one of the delegate functions instead of here. See this.

Community
  • 1
  • 1
hackerinheels
  • 1,141
  • 1
  • 6
  • 14
0

hackerinheels 's (3) is right. reloadData hasn't finished when scrolling table is called. So I added the code:

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section == ([self.tableView numberOfSections] - 1)) {
        //NSLog(@"finish loading");
        // put the scrolling table call here:   [self scrollTableView ];
    }
    return  nil;
}

Thank you all for helping!