首先想自定义uisearchviewcontroller是看(https://github.com/iphone5solo/PYSearch)的源码以及iOS自定义转场动画实战讲解(http://www.jianshu.com/p/ea0132738057)这篇博客后出现的想法。可能里面会有很多理解有误的地方,水平有限,欢迎指正。项目里面还用到MJRefresh,只是用于列表刷新,可有可无。
1.项目github地址
https://github.com/FledglingBoy/TESTSearchViewController
2.显示效果
2017-10-18 16_11_50.gif3.项目讲解
1. AJHYSearchViewController的实现
首先要自己写一个viewcontroller(AJHYSearchViewController)用于显示搜索结果,为了增加AJHYSearchViewController的通用性,自定义UITableViewDataSource协议和UITableViewDelegate协议,这样可以自由选择在AJHYSearchViewController里面实现UITableViewDataSource代理和UITableViewDelegate代理,还是在跳转搜索的页面实现这两个代理。
具体代码如下:
//自定义协议
@protocol AJSearchViewDataSource <NSObject, UITableViewDataSource>
@required
- (UITableViewCell *)searchView:(UITableView *)searchView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
- (NSInteger)searchView:(UITableView *)searchView numberOfRowsInSection:(NSInteger)section;
@optional
- (NSInteger)numberOfSectionsInSearchView:(UITableView *)searchView;
- (CGFloat)searchView:(UITableView *)searchView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
@end
@protocol AJSearchViewControllerDelegate <NSObject, UITableViewDelegate>
@optional
- (void)didSelectSearchAtIndexPath:(NSIndexPath *)indexPath;
- (void)searchViewController:(AJHYSearchViewController *)searchViewController
didSearchWithSearchBar:(UISearchBar *)searchBar
searchText:(NSString *)searchText;
- (void)searchViewController:(AJHYSearchViewController *)searchViewController
searchTextDidChange:(UISearchBar *)searchBar
searchText:(NSString *)searchText;
- (void)searchViewController:(AJHYSearchViewController *)searchViewController didSelectSearchSuggestionAtIndexPath:(NSIndexPath *)indexPath
searchBar:(UISearchBar *)searchBar;
- (void)didClickCancel:(AJHYSearchViewController *)searchViewController;
//-(void)didRefreshHeader;
//-(void)didRefreshFooter;
@end
//AJHYSearchViewController代理实现,这里只列举一个,详细的可以看源码
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if ([self.dataSource respondsToSelector:@selector(numberOfSectionsInSearchView::)]) {
return [self.dataSource numberOfSectionsInSearchView:tableView];
}
return 1;
}
2.转场动画的实现
转场动画的实现步骤原理可以查看http://www.jianshu.com/p/ea0132738057这个博客,里面讲得还是很清楚的。我这里用的是最简单的非交互式动画。
网友评论