101在检索框中添加一个书签按钮(扩展知识:在检索框中添加一个范围条)

效果如下:

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     _searchBar.showsBookmarkButton = YES; //是否显示书签按钮;默认值是NO

 45     

 46     _searchBar.scopeButtonTitles = @[@"1", @"2", @"3", @"4", @"5"];

 47     _searchBar.showsScopeBar = YES; //是否显示范围条;默认值是NO

 48     _searchBar.selectedScopeButtonIndex = -1;

 49     

 50     [_searchBar sizeToFit]; //设置宽高大小自适应

 51     self.tableView.tableHeaderView = _searchBar;

 52     

 53 }

 54 

 55 - (void)loadNavigation {

 56     self.navigationItem.title = @"在检索框中添加一个书签按钮";

 57 }

 58 

 59 - (void)loadData {

 60     _mArrDataSourceOfTableView = [[NSMutableArray alloc] initWithCapacity:kCount];

 61     _mArrDataSourceOfSearchBar = [[NSMutableArray alloc] initWithCapacity:kCount];

 62     for (NSInteger i=0; i<kCount; i++) {

 63         [_mArrDataSourceOfTableView addObject:[NSString stringWithFormat:@"%ld", (long)i]];

 64         _mArrDataSourceOfSearchBar[i] = _mArrDataSourceOfTableView[i];

 65     }

 66 }

 67 

 68 - (void)updateTableView:(NSString *)searchText {

 69     [_mArrDataSourceOfSearchBar removeAllObjects];

 70     if (searchText.length == 0) {

 71         _mArrDataSourceOfSearchBar = [[NSMutableArray alloc] initWithArray:_mArrDataSourceOfTableView];

 72     } else {

 73         for (NSString *item in _mArrDataSourceOfTableView) {

 74             if ([item hasPrefix:searchText]) {

 75                 [_mArrDataSourceOfSearchBar addObject:item];

 76             }

 77         }

 78     }

 79     //表格视图tableView更新

 80     [self.tableView reloadData];

 81 }

 82 

 83 - (void)showAlert:(NSString *)msg {

 84     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示信息"

 85                                                     message:msg

 86                                                    delegate:self

 87                                           cancelButtonTitle:nil

 88                                           otherButtonTitles:@"确定", nil];

 89     [alert show];

 90 }

 91 

 92 #pragma mark - SearchBar

 93 - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {

 94     [self updateTableView:searchText];

 95     

 96     searchBar.selectedScopeButtonIndex = -1;

 97 }

 98 

 99 - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {

100     [self updateTableView:searchBar.text];

101     //隐藏键盘

102     [_searchBar resignFirstResponder];

103     

104     searchBar.selectedScopeButtonIndex = -1;

105 }

106 

107 - (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar {

108     [self showAlert:@"点击了书签按钮"];

109 }

110 

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

112     //selectedScope = searchBar.selectedScopeButtonIndex

113     NSString *selectedScopeTitle = searchBar.scopeButtonTitles[selectedScope];

114     NSString *msg = [NSString stringWithFormat:@"点击了范围条;选中的项索引位置=%ld,选中的项值=%@",

115                      (long)selectedScope,

116                      selectedScopeTitle];

117     [self showAlert:msg];

118     

119     searchBar.text = selectedScopeTitle;

120     [self updateTableView:searchBar.text];

121 }

122 

123 #pragma mark - TableView

124 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

125     return [_mArrDataSourceOfSearchBar count];

126 }

127 

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

129     static NSString *cellIdentifier = @"cellIdentifier";

130     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

131     if (!cell) {

132         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

133     }

134     cell.textLabel.text = _mArrDataSourceOfSearchBar[indexPath.row];

135     return cell;

136 }

137 

138 @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

 

你可能感兴趣的:(按钮)