iOS学习笔记之UI-UISearchController-And-UITableView

来源:互联网 发布:淘宝拉夏贝尔代购 编辑:程序博客网 时间:2024/06/04 18:28
//前言:从java转到iOS各种心酸苦辣,这里全都是坑阿//今天学到表视图与搜索框,觉得有必要与大家分享一下,这个小demo是        纯手写没有storyboard入口,是调用window创建的controller- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    // Override point for customization after application launch.    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    self.window.backgroundColor = [UIColor whiteColor];    self.window.rootViewController = [[ViewController alloc] init];    [self.window makeKeyAndVisible];    return TRUE;}//tableView实现的两个协议 UITableViewDataSource,UITableViewDelegate //UISearchController实现的两个协议    _searchController.searchResultsUpdater = self;    _searchController.searchBar.delegate = self;#import "ViewController.h"#define cellHight 90@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate,UISearchResultsUpdating>//搜索控制器@property (strong, nonatomic)UISearchController *searchController;//所有数据@property (strong,nonatomic)NSArray *listTeams;//筛选过的数据@property (strong, nonatomic)NSMutableArray *listFilterTeams;//筛选方法- (void)filterContentForSearchText:(NSString*)searchText scope:(NSInteger)scope;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    //获取所有数据    _listTeams = [[NSArray alloc]initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"heros" ofType:@"plist"]];    //初始化数据    [self filterContentForSearchText:@"" scope:-1];    //初始化控制器    _searchController = [[UISearchController alloc] initWithSearchResultsController:nil];    //设置搜索框为灰色    _searchController.dimsBackgroundDuringPresentation = false;    //设置搜索分类    _searchController.searchBar.scopeButtonTitles = @[@"中文",@"英文"];    //设置searchResultsUpdater委托代理    _searchController.searchResultsUpdater = self;    //设置searchBarDelegate委托代理    _searchController.searchBar.delegate = self;    //在表示图头部添加搜索框    self.tableView.tableHeaderView = _searchController.searchBar;    [_searchController.searchBar sizeToFit];    // Do any additional setup after loading the view, typically from a nib.}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}#pragma mark --内容过滤- (void)filterContentForSearchText:(NSString *)searchText scope:(NSInteger)scope{    if (searchText.length == 0) {        _listFilterTeams = [NSMutableArray arrayWithArray:_listTeams];        return;    }    //持久化查询    NSPredicate *predicte;    //筛选后临时数组    NSArray *teamArray;    switch (scope) {        case 0:            //包含查询            predicte = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@",searchText];            teamArray = [_listTeams filteredArrayUsingPredicate:predicte];            _listFilterTeams = [NSMutableArray arrayWithArray:teamArray];            break;        case 1:            predicte = [NSPredicate predicateWithFormat:@"SELF.nameEnglish contains[c] %@",searchText];            teamArray = [_listTeams filteredArrayUsingPredicate:predicte];            _listFilterTeams = [NSMutableArray arrayWithArray:teamArray];            break;        default:            _listFilterTeams = [NSMutableArray arrayWithArray:_listTeams];            break;    }}#pragma mark --协议方法 返回row- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    //返回表行数    return _listFilterTeams.count;}#pragma mark --创建单元表cell- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    //创建单元格    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"search"];    if (cell == nil) {        //初始化单元格        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"search"];    }    //获取数据    NSDictionary *rowDiction = _listFilterTeams[indexPath.row];    cell.textLabel.text = rowDiction[@"name"];    cell.detailTextLabel.text = rowDiction[@"intro"];    cell.imageView.image = [UIImage imageNamed:rowDiction[@"icon"]];    //使副标题自动换行    cell.detailTextLabel.numberOfLines = 0;    //设置表单元样式    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;    return cell;}UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"search"];这个语句根据标识符TableSampleIdentifier寻找当前可以重用的UITableViewCell。当某行滑出当前可见区域后,我们重用它所对应的UITableViewCell对象,那么就可以节省内存和资源。#pragma mark --实现刷新协议- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope{    [self updateSearchResultsForSearchController:_searchController];}#pragma mark --实时刷新- (void)updateSearchResultsForSearchController:(UISearchController *)searchController{    [self filterContentForSearchText:searchController.searchBar.text scope:searchController.searchBar.selectedScopeButtonIndex];    //重新执行table相关协议函数    [self.tableView reloadData];}@end

初始化的表
搜索框

1 0
原创粉丝点击