zoukankan      html  css  js  c++  java
  • 101在检索框中添加一个书签按钮(扩展知识:在检索框中添加一个范围条)

    效果如下:

    ViewController.h

    1 #import <UIKit/UIKit.h>
    2 
    3 @interface ViewController : UITableViewController<UISearchBarDelegate>
    4 @property (strong, nonatomic) UISearchBar *searchBar;
    5 @property (strong, nonatomic) NSMutableArray *mArrDataSourceOfTableView;
    6 @property (strong, nonatomic) NSMutableArray *mArrDataSourceOfSearchBar;
    7 
    8 @end

    ViewController.m

      1 #import "ViewController.h"
      2 
      3 @interface ViewController ()
      4 - (void)layoutUI;
      5 - (void)loadNavigation;
      6 - (void)loadData;
      7 - (void)updateTableView:(NSString *)searchText;
      8 - (void)showAlert:(NSString *)msg;
      9 @end
     10 
     11 @implementation ViewController
     12 #define kCount 64
     13 
     14 - (void)viewDidLoad {
     15     [super viewDidLoad];
     16     
     17     [self layoutUI];
     18 }
     19 
     20 - (void)didReceiveMemoryWarning {
     21     [super didReceiveMemoryWarning];
     22     // Dispose of any resources that can be recreated.
     23 }
     24 
     25 - (void)viewWillAppear:(BOOL)animated {
     26     [super viewWillAppear:animated];
     27     [self.navigationController setNavigationBarHidden:NO animated:animated];
     28     [self.navigationController setToolbarHidden:YES animated:animated];
     29 }
     30 
     31 #pragma mark - Private Methods
     32 - (void)layoutUI {
     33     [self loadNavigation];
     34     
     35     [self loadData];
     36     
     37     _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 50)];
     38     _searchBar.delegate = self;
     39     //_searchBar.prompt = @"数字查询";
     40     _searchBar.placeholder = @"请输入0-63之间的数字";
     41     _searchBar.keyboardType = UIKeyboardTypeNumberPad;
     42     _searchBar.barStyle = UIBarStyleDefault;
     43     _searchBar.tintColor = [UIColor blackColor];
     44 
     45     _searchBar.showsBookmarkButton = YES; //是否显示书签按钮;默认值是NO
     46     
     47     _searchBar.scopeButtonTitles = @[@"1", @"2", @"3", @"4", @"5"];
     48     _searchBar.showsScopeBar = YES; //是否显示范围条;默认值是NO
     49     _searchBar.selectedScopeButtonIndex = -1;
     50     
     51     [_searchBar sizeToFit]; //设置宽高大小自适应
     52     self.tableView.tableHeaderView = _searchBar;
     53     
     54 }
     55 
     56 - (void)loadNavigation {
     57     self.navigationItem.title = @"在检索框中添加一个书签按钮";
     58 }
     59 
     60 - (void)loadData {
     61     _mArrDataSourceOfTableView = [[NSMutableArray alloc] initWithCapacity:kCount];
     62     _mArrDataSourceOfSearchBar = [[NSMutableArray alloc] initWithCapacity:kCount];
     63     for (NSInteger i=0; i<kCount; i++) {
     64         [_mArrDataSourceOfTableView addObject:[NSString stringWithFormat:@"%ld", (long)i]];
     65         _mArrDataSourceOfSearchBar[i] = _mArrDataSourceOfTableView[i];
     66     }
     67 }
     68 
     69 - (void)updateTableView:(NSString *)searchText {
     70     [_mArrDataSourceOfSearchBar removeAllObjects];
     71     if (searchText.length == 0) {
     72         _mArrDataSourceOfSearchBar = [[NSMutableArray alloc] initWithArray:_mArrDataSourceOfTableView];
     73     } else {
     74         for (NSString *item in _mArrDataSourceOfTableView) {
     75             if ([item hasPrefix:searchText]) {
     76                 [_mArrDataSourceOfSearchBar addObject:item];
     77             }
     78         }
     79     }
     80     //表格视图tableView更新
     81     [self.tableView reloadData];
     82 }
     83 
     84 - (void)showAlert:(NSString *)msg {
     85     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示信息"
     86                                                     message:msg
     87                                                    delegate:self
     88                                           cancelButtonTitle:nil
     89                                           otherButtonTitles:@"确定", nil];
     90     [alert show];
     91 }
     92 
     93 #pragma mark - SearchBar
     94 - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
     95     [self updateTableView:searchText];
     96     
     97     searchBar.selectedScopeButtonIndex = -1;
     98 }
     99 
    100 - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    101     [self updateTableView:searchBar.text];
    102     //隐藏键盘
    103     [_searchBar resignFirstResponder];
    104     
    105     searchBar.selectedScopeButtonIndex = -1;
    106 }
    107 
    108 - (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar {
    109     [self showAlert:@"点击了书签按钮"];
    110 }
    111 
    112 - (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope {
    113     //selectedScope = searchBar.selectedScopeButtonIndex
    114     NSString *selectedScopeTitle = searchBar.scopeButtonTitles[selectedScope];
    115     NSString *msg = [NSString stringWithFormat:@"点击了范围条;选中的项索引位置=%ld,选中的项值=%@",
    116                      (long)selectedScope,
    117                      selectedScopeTitle];
    118     [self showAlert:msg];
    119     
    120     searchBar.text = selectedScopeTitle;
    121     [self updateTableView:searchBar.text];
    122     
    123     //从 UISearchBar 中获取到其对应的 UITextField 对象实例
    124     UITextField *txtSearch = [searchBar valueForKey:@"_searchField"];
    125     NSLog(@"txtSearch.text: %@, searchBar.text: %@ 值一样", txtSearch.text, searchBar.text);
    126 }
    127 
    128 #pragma mark - TableView
    129 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    130     return [_mArrDataSourceOfSearchBar count];
    131 }
    132 
    133 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    134     static NSString *cellIdentifier = @"cellIdentifier";
    135     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    136     if (!cell) {
    137         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    138     }
    139     cell.textLabel.text = _mArrDataSourceOfSearchBar[indexPath.row];
    140     return cell;
    141 }
    142 
    143 @end

    AppDelegate.h

    1 #import <UIKit/UIKit.h>
    2 
    3 @interface AppDelegate : UIResponder <UIApplicationDelegate>
    4 @property (strong, nonatomic) UIWindow *window;
    5 @property (strong, nonatomic) UINavigationController *navigationController;
    6 
    7 @end

    AppDelegate.m

     1 #import "AppDelegate.h"
     2 #import "ViewController.h"
     3 
     4 @interface AppDelegate ()
     5 @end
     6 
     7 @implementation AppDelegate
     8 
     9 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    10     _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    11     ViewController *viewController = [[ViewController alloc] init];
    12     _navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
    13     _window.rootViewController = _navigationController;
    14     //[_window addSubview:_navigationController.view]; //当_window.rootViewController关联时,这一句可有可无
    15     [_window makeKeyAndVisible];
    16     return YES;
    17 }
    18 
    19 - (void)applicationWillResignActive:(UIApplication *)application {
    20 }
    21 
    22 - (void)applicationDidEnterBackground:(UIApplication *)application {
    23 }
    24 
    25 - (void)applicationWillEnterForeground:(UIApplication *)application {
    26 }
    27 
    28 - (void)applicationDidBecomeActive:(UIApplication *)application {
    29 }
    30 
    31 - (void)applicationWillTerminate:(UIApplication *)application {
    32 }
    33 
    34 @end
  • 相关阅读:
    隔离级别 && SNAPSHOT
    多态性&& 虚函数 && 抽象类
    socket编程
    [APIO2015]巴邻旁之桥
    LuoguP3701 「伪模板」主席树
    线段树标记永久化
    [HNOI2015]开店
    NOIP2017划水记
    FFTNTT总结
    [THUWC 2017]在美妙的数学王国中畅游
  • 原文地址:https://www.cnblogs.com/huangjianwu/p/4579506.html
Copyright © 2011-2022 走看看