115search analyzerpp 怎么用

1396人阅读
从今天开始准备在CSDN记录做ios开发中进步的点滴,今天自己用系统的UISearchBar写了一个搜索的功能
核心代码在下面贴出,
几个麻烦的地方 一个是searchBar的左右两边有一块灰色的区域影响效果 可以把颜色做如下设置
&UISearchBar *searchBar = [[UISearchBar&alloc]initWithFrame:CGRectMake(3,2,self.view.frame.size.width-6,30)];
& & searchBar.backgroundColor = [UIColor&clearColor];
& & searchBar.placeholder =@&搜索&;
& & searchBar.barTintColor = [UIColor&whiteColor];
& & [searchBar.layer&setMasksToBounds:YES];
& & searchBar.layer.cornerRadius=0;
& & searchBar.layer.borderColor=[UIColor&lightGrayColor].CGColor;
& & searchBar.layer.borderWidth=0.5;
& & searchBar.delegate =self;
&& & searchBar.showsCancelButton =NO;
[self.view&addSubview:searchBar];
[[UITextField&appearance]&setTintColor:MainColor];
&&//,这种方法将影响所有
textField.tintColor&=&[UIColor&redColor];
如果在InterfaceBuilder中修改View的TintColor属性并不好用。
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
& & //[dataArray removeAllObjects];
& & [_myTable&reloadData];
& & //[searchBar setShowsCancelButton:YES animated:YES];
&& & searchBar.showsCancelButton =YES;
&& & NSLog(@&TextDidBeginEditing&);
& & //[[UITextField &appearance]&setTintColor:[UIColor blackColor]];
& & for (UIView *view&in [[searchBar.subviews&lastObject]subviews])
& & & & if ([view&isKindOfClass:[UIButton&class]])
& & & & & & UIButton *cancelBtn = (UIButton *)
& & & & & & [cancelBtn setTitle:@&取消&forState:UIControlStateNormal];
& & & & & & [cancelBtn setTitleColor:[UIColorblackColor]forState:UIControlStateNormal];
//在点击确定按钮的时候做处理数据请求,然后把搜索结果展示出来
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
& & NSLog(@&%@&,searchBar.text);
& & keyTitle = searchBar.text;
& & [dataArray&removeAllObjects];//点击确定按钮的时候 先清空之前的数据源数组,以便于存入最新的搜索结果
& & [self&httpRequest];//根据输入的关键字,从新请求数据 请求之后 刷新表
& & //[self.myTable reloadData];
1.UISearchBar(效果如下:)
①创建UISearchBar对象
//初始化,定义frame
UISearchBar *bar = [[UISearchBar alloc] initWithFrame:CGRectMake
(0, 50, self.view.frame.size.width, 80)];
//添加到控制器的视图上
[self.view addSubview:bar];②UISerachBar的属性
//autocapitalizationType:包含4种类型,但是有时候键盘会屏蔽此属.
//1.autocapitalizationType————自动对输入文本对象进行大小写设置.
 bar.autocapitalizationType = UITextAutocapitalizationTypeW
  //2.autocorrectionType————自动对输入文本对象进行纠错
bar.autocorrectionType = UITextAutocorrectionTypeY
//3.设置title
bar.prompt = @&全部联系人&;
//4.设置颜色
   bar.tintColor  = [UIColor purpleColor];//渲染颜色
   bar.barTintColor = [UIColor orangeColor];//搜索条颜色
bar.backgroundColor =  [UIColor purpleColor];//背景颜色,因为毛玻璃效果(transulent).
//5.translucent————指定控件是否会有透视效果
bar.translucent = YES;
//6.scopeButtonTitles(范围buttonTitle)
  
bar.scopeButtonTitles = @[@&精确搜索&,@&模糊搜索&];
    bar.selectedScopeButtonIndex = 1;//通过下标指定默认选择的那个选择栏
//7.控制搜索栏下部的选择栏是否显示出来(需设置为YES 才能使用scopebar)
bar.showScopeBar = YES;
//8.设置搜索栏右边的按钮
bar.showsSearchResultsButton  = YES;//向下的箭头
bar.showsCancelButton = YES; //取消按钮
bar.showsBookmarkButton =  YES; //书签按钮
//9.提示内容
bar.placeholder = @&搜索&;
//10.取消键盘操作
[searchBar resignFirstResponder]; 
//11.设置代理
//UISearchBar不执行搜索行为,必须使用delegate,当输入搜索文本、点击button按钮后,代理的方法
会完成搜索对应的操作。
//.控件的委托,委托要遵从UISearchBarDelegate协议,默认是nil
bar.delegate =
③代理要试实现的协议方法
1).输入编辑事件处理
– searchBar:textDidChange:
– searchBar:shouldChangeTextInRange:replacementText:
– searchBarShouldBeginEditing:
– searchBarTextDidBeginEditing:
– searchBarShouldEndEditing:
– searchBarTextDidEndEditing:2).点击按钮事件处理– searchBarBookmarkButtonClicked:
– searchBarCancelButtonClicked:
– searchBarSearchButtonClicked:
– searchBarResultsListButtonClicked:3).点击Scope按钮事件处理– searchBar:selectedScopeButtonIndexDidChange:&/span&
2.UISearchDisplayController(注:iOS8以上已经弃用)
结合UISearchBar实现效果如下,实现搜索功能.
提示:检测Xcode系统版本代码如下:
[[[UIDevice currentDevice] systemVersion] floatValue] &= 8.0 ? YES: NO;①.创建对象
//需要创建UISearchBar对象,这里将对象都定义成了属性
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
//设置UISearchBar属性,上面有UISearchBar详细介绍.
    self.searchBar.placeholder = @&enter province name&;
    self.searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
   self.searchBar.autocapitalizationType = UITextAutocapitalizationTypeAllC
    self.searchBar.scopeButtonTitles = [NSArray arrayWithObjects:
@&All&,@&A&,@&B&,@&C&,@&D& ,nil];
    self.searchBar.showsScopeBar = YES;
    self.searchBar.keyboardType = UIKeyboardTypeNamePhoneP
    self.searchBar.showsBookmarkButton = YES;
//将seachBar作为控制器的透视图,视图控制器,继承UITableViewController
    self.tableView.tableHeaderView = _searchB
//将UIsearchBar添加到UIdSearchDispalyController上
self.displayController = [[UISearchDisplayController alloc]
 initWithSearchBar:_searchBar contentsController:self];注:searchBar————在searchdisplaycontroller初始化后,searchbar是不可修改的,是readonly属性的.
②配置UISearchDisplayController的属性
//active————是搜索界面可视化,默认为no,可用setActive方法设置.
self.displayController.active = YES;
    // searchResultsDataSource————搜索结果的数据源,代理对象(UITableViewDataSource)
    self.displayController.searchResultsDataSource =
//searchResultsDelegate————搜索结果的委托,服从协议UITableViewDelegate  
 self.displayController.searchResultsDelegate =③实现
/*searchDisplayController 自身有一个searchResultsTableView,
所以在执行操作的时候首先要判断是否是搜索结果的tableView,如果是显示的就是搜索结果的数据,
如果不是,是TableView自身的view,则需要显示原始数据。*/
if (tableView == self.tableView) {
return self.dataArray.
NSPredicate *predicate = [NSPredicate predicateWithFormat:
@&self contains [cd] %@&,self.searchBar.text];
[[NSMutableArray alloc] initWithArray:
[self.dataArray filteredArrayUsingPredicate:predicate]];
return self.arr.
④使用UISearchDisplayDelegate的委托方法进行搜索操作:
1).搜索状态改变事件处理方法:
– searchDisplayControllerWillBeginSearch:
– searchDisplayControllerDidBeginSearch:
– searchDisplayControllerWillEndSearch:
– searchDisplayControllerDidEndSearch:
2).装载和卸载tableview事件处理方法:
– searchDisplayController:didLoadSearchResultsTableView:
– searchDisplayController:willUnloadSearchResultsTableView:
3).显示和隐藏tableview事件处理方法:
– searchDisplayController:willShowSearchResultsTableView:
– searchDisplayController:didShowSearchResultsTableView:
– searchDisplayController:willHideSearchResultsTableView:
– searchDisplayController:didHideSearchResultsTableView:4).搜索条件改变时响应事件处理方法:– searchDisplayController:shouldReloadTableForSearchString:
– searchDisplayController:shouldReloadTableForSearchScope
3.UISearchController(iOS8新特性)
UISearchController实现和上述效果基本一致,适用于iOS8以上版本
实现如下图搜索效果
1)新建控制器,继承与UITableViewController,在extension中定义属性
//存储原来的数据
@property (nonatomic, retain) NSArray *dataA
//存储检索后的数据
@property (nonatomic, retain) NSArray *
2).加载数据,懒加载- (NSArray *)dataArr {
if (!_dataArr) {
 self.dataArr = [NSArray arrayWithObjects:
@&Allan&,@&Abbbb&,@&Acccc&,@&Bccccc&,@&Cddddffk&,@&Cddkllll&,
@&Ekkflfl&,@&Ekljljfg& ,@&Leslie&,@&Mm&,@&Meinv&,@&Meihi&,@&Catilin&,
@&Arron&, @&211&, @&232&, @&243&, @&264&, @&285&, @&106&, @&311&,
@&432&, @&543&, @&664&, @&785&, @&806&, nil];
return _dataA
//如果检索后的数据为空,将原有数据赋值给检索数据
- (NSArray *)arr {
    if (!_arr) {
        self.arr = self.dataA
    }
    return _
3.加载UISearchController对象- (void)viewDidLoad {
[super viewDidLoad];
//cell重用机制,调用系统的
[self.tableView registerClass:[UITableViewCell class]
 forCellReuseIdentifier:@&lock&];
//创建搜索条,将自身设置为展示结果的控制器
UISearchController *searchVC =[[UISearchController alloc]
initWithSearchResultsController:nil];
//设置渲染颜色
searchVC.searchBar.tintColor = [UIColor orangeColor];
//设置状态条颜色
searchVC.searchBar.barTintColor = [UIColor orangeColor];
//设置开始搜索时背景显示与否(很重要)
searchVC.dimsBackgroundDuringPresentation = NO;
//适应整个屏幕
 [searchVC.searchBar sizeToFit];
//设置显示搜索结果的控制器
searchVC.searchResultsUpdater = //协议(UISearchResultsUpdating)
//将搜索控制器的搜索条设置为页眉视图
self.tableView.tableHeaderView = searchVC.searchB
4).实现协议中的方法,必须实现
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
//谓词检测
NSPredicate *predicate = [NSPredicate predicateWithFormat:
@&self contains [cd] %@&, searchController.searchBar.text];
//将所有和搜索有关的内容存储到arr数组
 self.arr = [NSMutableArray arrayWithArray:
[self.dataArr filteredArrayUsingPredicate:predicate]];
//重新加载数据
   [self.tableView reloadData]; 
5).设置UITabelViewController中的其它
#pragma mark - Table view data source
//设置有多少个分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
//每个分区有多少行数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
(NSInteger)section {
return self.arr.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath {
self.cell = [tableView dequeueReusableCellWithIdentifier:@&lock&
forIndexPath:indexPath];
//设置cell上展示的内容,即搜索后的数据
self.cell.textLabel.text = self.arr[indexPath.row];
return self.
}注.以上是实现搜索框搜索空能.(当搜索内容为空时,返回的时所有数据,如果搜索内容为空,返回空时,需要进行其它修改操作.)
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:51459次
积分:1341
积分:1341
排名:千里之外
原创:63篇
转载:143篇
(1)(2)(2)(4)(6)(10)(10)(17)(23)(17)(5)(9)(27)(30)(19)(13)(12)经检测你所在的网络可能存在爬虫,因资源限制,我们只能拒绝你的请求。
如果你是推酷的用户,可以以继续访问使用。
如有疑问,可将IP信息发送到
请求解封。

我要回帖

更多关于 115search怎么用不起 的文章

 

随机推荐