add搜索栏

UISearchDisplayController类

 

1)拖动搜索栏到视图中

此时它与列表项在一个controller视图中,且它与列表项共享同一个controller类,则一定会调用两个函数。- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath和- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section

 

2)实现搜索过滤器

1. 实现方法过滤菜单名称,返回正确的搜索结果;

2. 更改数据源方法,区分不同的表视图。

操作:

1)NSArray *searchResults;//新建搜索结果的数组

2)添加方法- (void)filterContentForSearchText:(NSString*)searchTextscope:(NSString*)scope{

NSPredicate *resultPredicate = [NSPredicatepredicateWithFormat:@"SELF contains[cd] %@",searchText];

searchResults =[recipes filteredArrayUsingPredicate:resultPredicate];

}存放到数组中

 

3)实现搜索显示控制器(Search Display Controller)委托

-(BOOL)searchDisplayController:(UISearchDisplayController*)controller shouldReloadTableForSearchString:(NSString *)searchString
{

[selffilterContentForSearchText:searchStringscope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBarselectedScopeButtonIndex]]];

return YES;

}

4)在searchResultsTableView 显示搜索结果

修改控制器必定要执行的那两个函数

-(NSInteger)tableView:(UITableView *)tableViewnumberOfRowsInSection:(NSInteger)section{

   

    if (tableView == self.searchDisplayController.searchResultsTableView){

        return [searchResults count];

    }else{

    return [countArray count];

    }

}

 

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{

 

    static NSString *simpleTableIdentifier =@"RecipeCell";

    UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:simpleTableIdentifier];

  

    if (cell == nil) {

        cell = [[UITableViewCell alloc]

                initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:simpleTableIdentifier];

    }

   

    if (tableView ==self.searchDisplayController.searchResultsTableView) {

          cell.textLabel.text = [searchResultsobjectAtIndex:indexPath.row];

    }else{

         cell.textLabel.text = [countArrayobjectAtIndex:indexPath.row];

    }

   

    return cell;

}

到此,输入相应值后,会出现想要的搜索结果。但是点击后无反应。

 

处理搜索结果中的行选择

1.新增函数

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSLog(@"tableView didSelectRowAtIndexPath");

 

    if (tableView == self.searchDisplayController.searchResultsTableView) {

        [selfperformSegueWithIdentifier: @"showDetail"sender: self ];//此方法译为:当手点击此控件后触发showDetail联线

    }

}

2.更改函数

-(void)prepareForSegue:(UIStoryboardSegue*)segue sender:(id)sender{

    NSLog(@"prepareForSegue sender");

 

    if ([segue.identifierisEqualToString:@"showDetail"]) {

      

        ExPageDetailViewController* eControl =segue.destinationViewController;

        NSIndexPath* path = nil;

       

        if ([self.searchDisplayControllerisActive]) {

            path =[self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];

            eControl.information =[searchResults objectAtIndex:path.row];

        }else{

       

            path = [self.tableViewindexPathForSelectedRow];

           

            eControl.information = [countArrayobjectAtIndex:path.row];

        }

        eControl.hidesBottomBarWhenPushed =YES;//eControl控制器中的tar bar隐藏。

    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值