UITableViewController-分区和索引
2015-06-23 23:50
615 查看
// // MyIndexTabelViewController.h // AppUI组件学习 // // Created by 麦子 on 15/6/23. // Copyright (c) 2015年 麦子. All rights reserved. // #import <UIKit/UIKit.h> @interface MyIndexTabelViewController : UITableViewController<UISearchBarDelegate> @property(nonatomic,strong) NSMutableArray *array; @property(nonatomic,strong) NSMutableDictionary *dic; @property(nonatomic,strong) NSMutableArray *searchArray; @property(nonatomic,strong) NSMutableArray *tempArray; @end
// // MyIndexTabelViewController.m // AppUI组件学习 // // Created by 麦子 on 15/6/23. // Copyright (c) 2015年 麦子. All rights reserved. // #import "MyIndexTabelViewController.h" @implementation MyIndexTabelViewController{ BOOL searchFlag; } @synthesize array; @synthesize dic; @synthesize searchArray; @synthesize tempArray; - (void)viewDidLoad{ [super viewDidLoad]; self.title = @"分区和索引"; [self createView]; } - (void)createView{ // 获取plist的文件 NSString *path = [[NSBundle mainBundle] pathForResource:@"NbaPlayer" ofType:@"plist"]; self.dic = [NSMutableDictionary dictionaryWithContentsOfFile:path]; self.array = (NSMutableArray *)[[self.dic allKeys]sortedArrayUsingSelector:@selector(compare:)]; // 组装数据 self.searchArray = [[NSMutableArray alloc] init]; NSArray *dataArray = [self.dic allValues]; for (NSArray *playerNameArray in dataArray) { for (NSString *str in playerNameArray) { [self.searchArray addObject:str]; } } self.tempArray = self.searchArray; // 下拉控件 self.refreshControl = [[UIRefreshControl alloc] init]; [self.refreshControl addTarget:self action:@selector(refreshEvent) forControlEvents:UIControlEventValueChanged]; self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"努力加载中....."]; CGFloat width = [[UIScreen mainScreen] bounds].size.width; // 搜索栏 UISearchBar *search = [[UISearchBar alloc] init]; search.frame = CGRectMake(0,0,width,40); // 关掉拼音检查 search.autocorrectionType = UITextAutocorrectionTypeYes; // 取消首字母大写 search.autocapitalizationType = UITextAutocapitalizationTypeNone; // 显示取消按钮 search.showsCancelButton = NO; search.placeholder = @"请输入你要查找的内容"; // 代理处理 search.delegate = self; self.tableView.tableHeaderView = search; } // 监听下拉 - (void)refreshEvent{ self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"加载成功....."]; [self.refreshControl endRefreshing]; // 结束刷新 [self.tableView reloadData]; // 重新加载数据 } // 分区 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ if (searchFlag) { return 1; } return self.array.count; } // 一区有几行 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ if (searchFlag) { return self.searchArray.count; } NSString *key = [self.array objectAtIndex:section]; NSArray *childArray = [self.dic objectForKey:key]; return childArray.count; } // 单元头 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ if (searchFlag) { return @"查询结果"; } return self.array[section]; } //单元格 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myCell"]; } if (searchFlag) { cell.textLabel.text = self.searchArray[indexPath.row]; searchFlag = false; return cell; } NSString *key = [self.array objectAtIndex:indexPath.section];// 获取区 NSArray *dataArray = [[self.dic objectForKey:key]sortedArrayUsingSelector:@selector(compare:)]; // 默认排序 cell.textLabel.text = dataArray[indexPath.row]; return cell; } // 添加索引 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{ if (searchFlag) { return nil; } return self.array; } // 搜索按钮--搜索 - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{ // UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"搜索栏" message:@"测试搜索点击按钮" delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:@"已经看到", nil]; // [alert show]; searchFlag = true; self.searchArray = self.tempArray; NSString *searchText = searchBar.text; NSMutableArray *findArray = [[NSMutableArray alloc] init]; for (NSString *str in self.searchArray) { if ([[str uppercaseString] containsString:searchText] || [[str lowercaseString] containsString:searchText]) { [findArray addObject:str]; } } self.searchArray = findArray; [self.tableView reloadData]; } // 开始编辑的时候 - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{ searchBar.showsCancelButton = YES; } // 点击取消按钮的 - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{ // 去掉聚焦 [searchBar resignFirstResponder]; searchBar.showsCancelButton = NO; self.searchArray = self.tempArray; searchFlag = false; [self.tableView reloadData]; } @end
相关文章推荐
- UITableViewController显示UIToolbar
- UItableViewController
- iOS6新特征:UIRefreshControl[下拉刷新]使用示例
- iOS之UITableViewController的使用
- iOS之UITableView的使用——静态单元格
- iOS之UITableView的使用—下拉刷新
- UITableViewController-简单自定义单元格
- 区别dequeueReusableCellWithIdentifier 和dequeueReusableCellWithIdentifier: forIndexPath
- UITableViewController详解
- UITableViewController类预置模板详解(部分一)
- iOS开发之高级视图—— UITableView的简化操作——UITableViewController
- UISegmentedControl在storyboard下的使用,支持横评
- 在UITableViewController对应的storyBoard中添加view
- swift UItableView和自定义uitableViewCell
- UI基础之---- UITableView 编辑
- iOS开发系列--UITableView全面解析
- Errors running builder 'Android Resource Manager' on Project java.lang.NullPointerException
- An internal error occurred during: "Building workspace". java.lang.StackOverflowError
- An internal error occurred during: "Building workspace". java.lang.StackOverflowError