SearchController

本文介绍如何在iOS应用中实现带有分类筛选的搜索功能,包括创建UISearchController实例、配置UISearchBar并实现搜索逻辑来更新搜索结果。

- (void)viewDidLoad {

    [super viewDidLoad];

    // 创建用于展示搜索结果的表vc实例

    self.showResultVC = [[ShowResultTableViewController alloc]init];

    // 创建搜索控制器的实例

    self.searchController = [[UISearchController alloc]initWithSearchResultsController:self.showResultVC];

    // 设置搜索条的尺寸为自适应

    [self.searchController.searchBar sizeToFit];

    // 设置搜索条中的分段类别

    self.searchController.searchBar.scopeButtonTitles = @[@"设备",@"软件",@"其他"];

    

    // 当前主控制器的视图的表头添加searchBar

    self.tableView.tableHeaderView = self.searchController.searchBar;

    // 设置搜索控制器的结果更新代理对象

    self.searchController.searchResultsUpdater = self;

    // 允许在当前界面上切换展示数据的上下文对象

    // 允许从主table切换为展示结果的table

    self.definesPresentationContext = NO;

    // 为了点击searchBar上的类别按钮时,也能有响应

    // 所以设置searchBar的代理

    self.searchController.searchBar.delegate = self;

}

 

#pragma mark - UISearchBarDelegate协议

// 只要选择了新的类别,重新调用下面的搜索方法再次比对

- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope

{

    [self updateSearchResultsForSearchController:self.searchController];

}

#pragma mark - UISearchResultUpdating协议

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController

{

    // 从委托方中获取用户在文本框中输入的文字内容

    NSString *searchText = searchController.searchBar.text;

    // 从委托方中获取用户选择的类别按钮

    NSInteger selecteonIndex = searchController.searchBar.selectedScopeButtonIndex;

    //self.allProducts中逐一比对数据

    NSMutableArray *resultArray = [NSMutableArray array];

    for (Product *p in self.allProducts) {

        NSRange range = [p.name rangeOfString:searchText];

        // 如果名称匹配 且类别相同

        // 则将此产品记录到结果数cv组中

        if (range.length>0 && p.type==selecteonIndex) {

            [resultArray addObject:p];

        }

    }

    // 将要展示的数据结果给 负责显示的vc传过去

    self.showResultVC.resultArray = resultArray;

    // 更新视图显示数据

    [self.showResultVC.tableView reloadData];

}

 

 

import {MyDataResource} from '../viewmodel/Data' import {fourValue} from '../viewmodel/dataModel' import {Four} from '../viewmodel/dataModel' @Component @Entry //'案例4:瀑布流组件' struct Page3 { private AM : MyDataResource = new MyDataResource() private search:SearchController = new SearchController() @State Png : Resource = $r('app.media.FourGray') @State SText : string = '' @State Index : number = -1 @State TextColor2 : ResourceColor = Color.Black @State WidthArray : Array<number> = [120 , 130 , 140 , 150 , 160 ,120 , 130 , 140 , 150 , 160 ,120 , 130 , 140 , 150 , 160 ] @State HeightArray : Array<number> = [180 , 190 , 200 ,210 , 220, 180 , 190 , 200 ,210 , 220, 180 , 190 , 200 ,210 , 220] @State TextColor : Array<ResourceColor> = [Color.White , Color.Black , Color.Blue , Color.Brown , Color.Green , Color.Orange , Color.Pink ,Color.White , Color.Black , Color.Blue , Color.Brown , Color.Green , Color.Orange , Color.Pink , Color.Yellow ] @State textInput : string = '' aboutToAppear(): void { for (let i = 0 ; i < fourValue.length ; i++) { this.AM.AddData(fourValue[i]); let data : number = Math.random() * 10 this.WidthArray[data - 1] = this.WidthArray[data] this.HeightArray[data - 1] = this.HeightArray[data] this.TextColor[data - 1] = this.TextColor[data] } } scroller : Scroller = new Scroller() build() { //瀑布流 Stack() { Row() { Search({ value: this.textInput, placeholder: '请搜索', controller: this.search }) .onChange((value: string) => { this.textInput = value }) .width('87%') .height('100%') Text('搜索') .fontWeight(200) .layoutWeight(1) .padding({left:3}) .onClick(() => { for (let i = 0 ; i < fourValue.length ; i++) { if (this.AM.dataArray[i].FourText.substring(0 , 1) == this.textInput) { this.AM.AddData(fourValue[i]); console.
最新发布
03-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值