4
votes

I'm using a UITableView with an UISearchBar on top of table view, for me every thing is working fine except only one problem like if i search for a letter the search results in uitableview is getting filtered and its fine after that , if i scroll the search result till end of my tableview then if i press cancel button of my uisearchbar the tableview is refreshing all data but it is displaying not from the first cell, it is displaying from (approx 25th cell) like how much i scroll in my search result that much length after it is displaying after click of cancel.I don't know what is the problem i'm calling reload data also , i need to display tableview as it loads in beginning.

Any help is appreciated in advance.Thank You.

2

2 Answers

12
votes

In your - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar Delegate method which handles the Cancel button you could add a bit of code to scroll back to the top of the Table View like so

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    // ... Code to refresh the Table View

    // Reload the Table View
    [self.tableView reloadData];

    // Scroll to top
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

You may need to play with the scroll position. Instead of UITableViewScrollPositionTop you may need to use UITableViewScrollPositionMiddle or UITableViewScrollPositionBottom or even UITableViewScrollPositionNone

1
votes

Call the following method on your table view:

[self.tableView reloadData];

Then it refreshes your table view