简体   繁体   English

如何在单击UISearchBar的取消按钮后使用UISearchBar刷新我的UITableView?

[英]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. 我正在桌面视图上使用带有UISearchBar的UITableView,对我来说每件事情都工作得很好,除了一个问题,如果我搜索一个字母,uitableview中的搜索结果会被过滤,之后就好了,如果我滚动搜索结果直到我的tableview结束然后,如果我按下我的uisearchbar的取消按钮,tableview刷新所有数据,但它不显示从第一个单元格,它显示从(约25个单元格)像我在搜索中滚动多少点击取消后显示的结果很长。我不知道我正在调用重载数据是什么问题,我需要在开始时加载显示tableview。

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 Delegate方法中处理Cancel按钮你可以添加一些代码来滚动回到Table View的顶部,就像这样

- (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 而不是UITableViewScrollPositionTop你可能需要使用UITableViewScrollPositionMiddleUITableViewScrollPositionBottom甚至UITableViewScrollPositionNone

Call the following method on your table view: 在表视图上调用以下方法:

[self.tableView reloadData];

Then it refreshes your table view 然后它刷新你的表视图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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