简体   繁体   中英

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.

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

Call the following method on your table view:

[self.tableView reloadData];

Then it refreshes your table view

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM