137在搜索框中实现下拉列表效果(扩展知识:表格视图数据源为空数据时显示提示信息)

效果如下:

137在搜索框中实现下拉列表效果(扩展知识:表格视图数据源为空数据时显示提示信息)

ViewController.h

 1 #import <UIKit/UIKit.h>

 2 #import "DropDownListViewController.h"

 3 

 4 @interface ViewController : UITableViewController<UISearchBarDelegate, PassValueDelegate>

 5 @property (strong, nonatomic) UISearchBar *searchBar;

 6 @property (strong, nonatomic) NSMutableArray *mArrDataSourceOfTableView;

 7 @property (strong, nonatomic) NSMutableArray *mArrDataSourceOfSearchBar;

 8 @property (strong, nonatomic) DropDownListViewController *ddlViewController;

 9 @property (strong, nonatomic) UILabel *lblEmptyDataMsg;

10 

11 @end

ViewController.m

  1 #import "ViewController.h"

  2 

  3 @interface ViewController ()

  4 - (void)layoutUI;

  5 - (void)loadData;

  6 - (void)updateTableView:(NSString *)searchText;

  7 - (void)setDDLHidden:(BOOL)isHidden animated:(BOOL)animated;

  8 @end

  9 

 10 @implementation ViewController

 11 #define kRowCount 64

 12 

 13 - (void)viewDidLoad {

 14     [super viewDidLoad];

 15     

 16     [self layoutUI];

 17 }

 18 

 19 - (void)didReceiveMemoryWarning {

 20     [super didReceiveMemoryWarning];

 21     // Dispose of any resources that can be recreated.

 22 }

 23 

 24 #pragma mark - Private Methods

 25 - (void)layoutUI {

 26     [self loadData];

 27     

 28     self.navigationItem.title = @"在搜索框中实现下拉列表效果";

 29     

 30     _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 50)];

 31     _searchBar.delegate = self;

 32     _searchBar.prompt = @"数字查询";

 33     _searchBar.placeholder = @"请输入0-63之间的数字";

 34     _searchBar.keyboardType = UIKeyboardTypeNumberPad;

 35     _searchBar.barStyle = UIBarStyleDefault;

 36     _searchBar.tintColor = [UIColor blackColor];

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

 38     self.tableView.tableHeaderView = _searchBar;

 39     

 40     _ddlViewController = [[DropDownListViewController alloc] initWithStyle:UITableViewStylePlain];

 41     _ddlViewController.delegate = self;

 42     _ddlViewController.view.frame = CGRectMake(22.0, 68.0, 320.0, 0);

 43     [self.view addSubview:_ddlViewController.view];

 44     

 45     _lblEmptyDataMsg = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 50)];

 46     CGPoint newPoint = self.view.center;

 47     newPoint.y -= 25;

 48     _lblEmptyDataMsg.center = newPoint;

 49     _lblEmptyDataMsg.text = @"没有相关数据";

 50     _lblEmptyDataMsg.textColor = [UIColor grayColor];

 51     _lblEmptyDataMsg.textAlignment = NSTextAlignmentCenter;

 52     _lblEmptyDataMsg.font = [UIFont systemFontOfSize:20];

 53     _lblEmptyDataMsg.hidden = YES;

 54     [self.view addSubview:_lblEmptyDataMsg];

 55 }

 56 

 57 - (void)viewDidAppear:(BOOL)animated {

 58     [super viewDidAppear:animated];

 59     

 60     [self setDDLHidden:YES animated:NO];

 61 }

 62 

 63 - (void)loadData {

 64     _mArrDataSourceOfTableView = [[NSMutableArray alloc] initWithCapacity:kRowCount];

 65     _mArrDataSourceOfSearchBar = [[NSMutableArray alloc] initWithCapacity:kRowCount];

 66     for (NSInteger i=0; i<kRowCount; i++) {

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

 68         _mArrDataSourceOfSearchBar[i] = _mArrDataSourceOfTableView[i];

 69     }

 70 }

 71 

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

 73     [_mArrDataSourceOfSearchBar removeAllObjects];

 74     if (searchText.length == 0) {

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

 76     } else {

 77         for (NSString *item in _mArrDataSourceOfTableView) {

 78             if ([item hasPrefix:searchText]) {

 79                 [_mArrDataSourceOfSearchBar addObject:item];

 80             }

 81         }

 82     }

 83     //表格视图tableView更新

 84     [self.tableView reloadData];

 85     

 86     //设置当UITableView数据源为空数据时,显示提示信息标签_lblEmptyDataMsg

 87     _lblEmptyDataMsg.hidden = _mArrDataSourceOfSearchBar.count > 0;

 88 }

 89 

 90 - (void)setDDLHidden:(BOOL)isHidden animated:(BOOL)animated {

 91     NSInteger ddlHeight = isHidden ? 0 : 180;

 92     //声明并实现block代码块setHeightForDDL

 93     void (^setHeightForDDL)() = ^() {

 94         [_ddlViewController.view setFrame:CGRectMake(22.0, 68.0, 320.0, ddlHeight)];

 95     };

 96     

 97     if (animated) {

 98         //设置视图变化使用动画效果

 99         [UIView beginAnimations:nil context:nil];

100         [UIView setAnimationDuration:0.3];

101         setHeightForDDL();

102         [UIView commitAnimations];

103     } else {

104         setHeightForDDL();

105     }

106     

107 }

108 

109 #pragma mark - SearchBar

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

111     //必须设置为最顶层;否则在UITableView数据源为空数据后切换,会出现_ddlViewController.view不是最顶层的现象

112     [self.view bringSubviewToFront:_ddlViewController.view];

113     

114     _ddlViewController.searchText = searchText;

115     [self setDDLHidden:(searchText.length == 0) animated:YES];

116     [_ddlViewController updateData];

117     

118 }

119 

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

121     //隐藏自定义的下拉列表视图

122     [self setDDLHidden:YES animated:YES];

123     

124     [self updateTableView:searchBar.text];

125     //隐藏键盘

126     [_searchBar resignFirstResponder];

127 }

128 

129 #pragma mark - TableView

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

131     return [_mArrDataSourceOfSearchBar count];

132 }

133 

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

135     static NSString *cellIdentifier = @"cellIdentifier";

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

137     if (!cell) {

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

139     }

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

141     return cell;

142 }

143 

144 #pragma PassValueDelegate

145 - (void)passValue:(NSString *)value {

146     if (value) {

147         _searchBar.text = value;

148         [self searchBarSearchButtonClicked:_searchBar];

149     }

150 }

151 

152 @end

PassValueDelegate.h

1 @protocol PassValueDelegate <NSObject>

2 - (void)passValue:(NSString *)value;

3 

4 @end

DropDownListViewController.h

 1 #import <UIKit/UIKit.h>

 2 #import "PassValueDelegate.h"

 3 

 4 @interface DropDownListViewController : UITableViewController

 5 {

 6     @private

 7     NSString *_selectedText;

 8 }

 9 @property (strong, nonatomic) NSMutableArray *mArrResultList;

10 @property (copy, nonatomic) NSString *searchText;

11 @property (weak, nonatomic) id<PassValueDelegate> delegate;

12 

13 - (void)updateData;

14 

15 @end

DropDownListViewController.m

 1 #import "DropDownListViewController.h"

 2 

 3 @interface DropDownListViewController ()

 4 - (void)layoutUI;

 5 @end

 6 

 7 @implementation DropDownListViewController

 8 #define kRowCount 6

 9 #define kHeightForRow 30.0

10 

11 - (void)viewDidLoad {

12     [super viewDidLoad];

13     

14     [self layoutUI];

15 }

16 

17 - (void)didReceiveMemoryWarning {

18     [super didReceiveMemoryWarning];

19     // Dispose of any resources that can be recreated.

20 }

21 

22 - (void)layoutUI {

23     self.tableView.layer.borderColor = [UIColor blackColor].CGColor;

24     self.tableView.layer.borderWidth = 1.0;

25     

26     _mArrResultList = [[NSMutableArray alloc] initWithCapacity:kRowCount];

27     _searchText = nil;

28     _selectedText = nil;

29 }

30 

31 - (void)updateData {

32     [_mArrResultList removeAllObjects];

33     if (_searchText && _searchText.length > 0) {

34         [_mArrResultList addObject:_searchText];

35         for (NSInteger i=1; i<kRowCount; i++) {

36             [_mArrResultList addObject:[NSString stringWithFormat:@"%@%ld", _searchText, (long)i]];

37         }

38     }

39     

40     [self.tableView reloadData];

41 }

42 

43 #pragma mark - TableView

44 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

45     return nil;

46 }

47 

48 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

49     return 1;

50 }

51 

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

53     return (_searchText && _searchText.length > 0) ? kRowCount : 0;

54 }

55 

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

57     static NSString *cellIdentifier = @"cellIdentifier";

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

59     if (!cell) {

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

61     }

62     cell.textLabel.text = _mArrResultList[indexPath.row];

63     return cell;

64 }

65 

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

67     _selectedText = _mArrResultList[indexPath.row];

68     [_delegate passValue:_selectedText];

69 }

70 

71 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

72     return kHeightForRow;

73 }

74 

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

 

你可能感兴趣的:(下拉列表)