Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to refresh my UITableView with UISearchBar after click of cancel button of UISearchBar?

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.

like image 814
George Avatar asked Dec 12 '22 09:12

George


1 Answers

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

like image 184
Suhail Patel Avatar answered May 03 '23 16:05

Suhail Patel