在tableview上添加searchbar
来源:互联网 发布:linux分辨率只有两个 编辑:程序博客网 时间:2024/06/03 22:53
#import <UIKit/UIKit.h>#define COOKBOOK_PURPLE_COLOR [UIColor colorWithRed:0.20392fgreen:0.19607f blue:0.61176f alpha:1.0f]#define CRAYON_NAME(CRAYON) [[CRAYON componentsSeparatedByString:@"#"] objectAtIndex:0]#define CRAYON_COLOR(CRAYON) [self getColor:[[CRAYON componentsSeparatedByString:@"#"] lastObject]]#define DEFAULTKEYS [self.crayonColors.allKeys sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)]#define FILTEREDKEYS [self.filteredArray sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)]@interface TableListViewController : UITableViewController{NSMutableDictionary *crayonColors;NSArray *filteredArray;UISearchBar *searchBar;UISearchDisplayController *searchDC;}@property (retain) NSMutableDictionary *crayonColors;@property (retain) NSArray *filteredArray;@property (retain) UISearchBar *searchBar;@property (retain) UISearchDisplayController *searchDC;@end@implementation TableListViewController@synthesize crayonColors;@synthesize filteredArray;@synthesize searchBar;@synthesize searchDC;- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView { return 1; }// Via Jack Lucky- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{[self.searchBar setText:@""]; }- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section {// Normal tableif (aTableView == self.tableView) returnself.crayonColors.allKeys.count; // Search tableNSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@", self.searchBar.text];self.filteredArray = [self.crayonColors.allKeysfilteredArrayUsingPredicate:predicate];return self.filteredArray.count;}// Convert a 6-character hex color to a UIColor object- (UIColor *) getColor: (NSString *) hexColor{unsigned int red, green, blue;NSRange range;range.length = 2; range.location = 0; [[NSScanner scannerWithString:[hexColor substringWithRange:range]]scanHexInt:&red];range.location = 2; [[NSScanner scannerWithString:[hexColor substringWithRange:range]]scanHexInt:&green];range.location = 4; [[NSScanner scannerWithString:[hexColor substringWithRange:range]]scanHexInt:&blue]; return [UIColor colorWithRed:(float)(red/255.0f) green:(float)(green/255.0f) blue:(float)(blue/255.0f) alpha:1.0f];}- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{// Dequeue or create a cellUITableViewCellStyle style = UITableViewCellStyleDefault;UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:@"BaseCell"];if (!cell) cell = [[[UITableViewCell alloc] initWithStyle:stylereuseIdentifier:@"BaseCell"] autorelease]; // Retrieve the crayon and its colorNSArray *keyCollection = (aTableView == self.tableView) ?DEFAULTKEYS : FILTEREDKEYS;NSString *crayon = [keyCollection objectAtIndex:indexPath.row];cell.textLabel.text = crayon;if (![crayon hasPrefix:@"White"])cell.textLabel.textColor = [self.crayonColors objectForKey:crayon];elsecell.textLabel.textColor = [UIColor blackColor];return cell;}// Respond to user selections by updating tint colors- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {NSArray *keyCollection = (aTableView == self.tableView) ?DEFAULTKEYS : FILTEREDKEYS;NSString *crayon = [keyCollection objectAtIndex:indexPath.row];self.navigationController.navigationBar.tintColor = [self.crayonColors objectForKey:crayon];self.searchBar.tintColor = [self.crayonColors objectForKey:crayon];}- (void) viewDidLoad{// Prepare the crayon color dictionaryNSString *pathname = [[NSBundle mainBundle] pathForResource:@"crayons" ofType:@"txt" inDirectory:@"/"];NSArray *rawCrayons = [[NSString stringWithContentsOfFile:pathnameencoding:NSUTF8StringEncoding error:nil] componentsSeparatedByString:@"\n"];self.crayonColors = [NSMutableDictionary dictionary];for (NSString *string in rawCrayons) [self.crayonColors setObject:CRAYON_COLOR(string)forKey:CRAYON_NAME(string)]; // Create a search barself.searchBar = [[[UISearchBar alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)] autorelease];self.searchBar.tintColor = COOKBOOK_PURPLE_COLOR;self.searchBar.autocorrectionType = UITextAutocorrectionTypeNo;self.searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;self.searchBar.keyboardType = UIKeyboardTypeAlphabet;self.tableView.tableHeaderView = self.searchBar; // Create the search display controllerself.searchDC = [[[UISearchDisplayController alloc]initWithSearchBar:self.searchBar contentsController:self]autorelease];self.searchDC.searchResultsDataSource = self;self.searchDC.searchResultsDelegate = self;}@end@interface TestBedAppDelegate : NSObject <UIApplicationDelegate>@end@implementation TestBedAppDelegate- (void)applicationDidFinishLaunching:(UIApplication *)application { TableListViewController *tlvc = [[TableListViewController alloc]init];UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:tlvc];nav.navigationBar.tintColor = COOKBOOK_PURPLE_COLOR; UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreenmainScreen] bounds]];[window addSubview:nav.view];[window makeKeyAndVisible];}@endint main(int argc, char *argv[]){NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];int retVal = UIApplicationMain(argc, argv, nil,@"TestBedAppDelegate");[pool release];return retVal;}
0 0
- 在tableview上添加searchbar
- 在navigationBar上添加searchBar 并设置字体
- 在tableview上添加悬停窗口
- 给TableView增加SearchBar
- 在有导航条的页面上添加tableView
- iOS 竖向tableView上添加横向tableView
- TableView基础(view上添加tableView)
- searchbar在点击屏幕上空白区域后收起键盘
- iOS开发 给TableView增加SearchBar
- iOS 在tableView上添加button导致按钮没有点击效果和不能滑动的问题
- 关于在tableview的footerView上添加button的点击不响应事件
- 故事版xib --view上添加tableview
- 为表视图添加searchBar
- 在tableview中添加便捷按钮
- TableView在iOS上的性能优化
- iOS 在tableView上消退键盘
- tableView在iPad上不能铺满屏幕
- TableView在iOS上的性能优化
- POSTGRESQL 数据库 系统维护
- java jre 中导入导出证书
- VPMN业务说明
- 带有导航控制器的页面隐藏上个界面下个界面也会消失解决办法
- SQL SERVER 2005、2008终极还原办法
- 在tableview上添加searchbar
- HTML5文件API
- 大话设计模式C++实现-第3.4.5.11.22.23章-设计原则
- Sql Server 删除所有用户表
- 类的嵌套
- 解决html静态页面在tomcat下中文乱码的问题。
- POSTGRESQL 数据库 数据库管理
- UIView自动调整控件(一个很方便使用的工具类吧)
- Linux下Android ADB驱动安装详解