xcode 单击 UISearchBar 的取消按钮后,如何使用 UISearchBar 刷新我的 UITableView?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7427810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to refresh my UITableView with UISearchBar after click of cancel button of UISearchBar?
提问by George
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.
提前感谢任何帮助。谢谢。
回答by Suhail Patel
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 方法中,您可以添加一些代码来滚动回表视图的顶部,如下所示
- (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
你可能需要使用UITableViewScrollPositionMiddle
或者UITableViewScrollPositionBottom
甚至UITableViewScrollPositionNone
回答by Srinivas
Call the following method on your table view:
在您的表视图上调用以下方法:
[self.tableView reloadData];
Then it refreshes your table view
然后它刷新你的表格视图