scroll UITableView

  • I have an UITableView with several searchOptions (like: "Starts with", "Ends with", etc.).

    When the user changes the search option, the UISearchDisplayDelegate implements:

    - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
    {
    // create some data array with stuff, depending on searchOption
    return YES;
    }

    Works fine.
    But: when one scrolls in the table and then switches the searchOption the table shows the correct items, but at some arbitrary scroll position.

    So I added this line in the above method:
    [ self performSelector: @selector(scrollToTop:) withObject: controller.searchResultsTableView  afterDelay: 0 ];

    - (void)scrollToTop: (UITableView *)tableView
    {
    NSIndexPath *indexPath = [ NSIndexPath indexPathForRow: 0 inSection: 0 ];
    [ tableView scrollToRowAtIndexPath: indexPath  atScrollPosition: UITableViewScrollPositionTop  animated: NO ];
    }

    Seems to be working.
    But: is this the correct way?

    One (minor) problem:
    The UITableViewDataSource gets the message:  tableView:cellForRowAtIndexPath:
    first with some arbitrary rows, and then again with rows 7, 6, 5, ..., 0.
    Some (small) waste of battery.

    Gerriet.

    P.S.
    I changed searchDisplayController:shouldReloadTableForSearchScope: to return NO and added [tableView reloadData]; to my scrollToTop: method.
    No more wasted row display.
    But still I am not sure whether this is the right way.
previous month may 2013 next month
MTWTFSS
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    
Go to today