-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { if([searchText length] == 0) { [displayObject removeAllObjects]; [displayObject addObjectsFromArray:allObject]; } else { [displayObject removeAllObjects]; for(NSString * string in allObject) { NSRange r = [string rangeOfString:searchText options:NSCaseInsensitiveSearch]; if(r.location != NSNotFound) { [displayObject addObject:string]; } } } [tableViewObj reloadData]; }
// // ViewController.h // searchBarApp // // Created by Weerachai on 11/18/55 BE. // Copyright (c) 2555 Weerachai. All rights reserved. // #import <UIKit/UIKit.h> @interface ViewController : UIViewController <UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate> { IBOutlet UISearchBar *searchBarObj; IBOutlet UITableView *tableViewObj; } @end
// // ViewController.m // searchBarApp // // Created by Weerachai on 11/18/55 BE. // Copyright (c) 2555 Weerachai. All rights reserved. // #import "ViewController.h" @interface ViewController () { NSMutableArray *allObject; NSMutableArray *displayObject; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. allObject = [[NSMutableArray alloc] init]; [allObject addObject:@"1. One"]; [allObject addObject:@"2. Two"]; [allObject addObject:@"3. Three"]; [allObject addObject:@"4. Four"]; [allObject addObject:@"5. Five"]; [allObject addObject:@"6. Six"]; [allObject addObject:@"7. Seven"]; [allObject addObject:@"8. Eight"]; [allObject addObject:@"9. Nine"]; [allObject addObject:@"10. Ten"]; displayObject =[[NSMutableArray alloc] initWithArray:allObject]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return displayObject.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { // Use the default cell style. cell = [[[UITableViewCell alloc] initWithStyle : UITableViewCellStyleSubtitle reuseIdentifier : CellIdentifier] autorelease]; } NSString *cellValue = [displayObject objectAtIndex:indexPath.row]; cell.textLabel.text = cellValue; return cell; } -(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { if([searchText length] == 0) { [displayObject removeAllObjects]; [displayObject addObjectsFromArray:allObject]; } else { [displayObject removeAllObjects]; for(NSString * string in allObject) { NSRange r = [string rangeOfString:searchText options:NSCaseInsensitiveSearch]; if(r.location != NSNotFound) { [displayObject addObject:string]; } } } [tableViewObj reloadData]; } -(void) searchBarSearchButtonClicked:(UISearchBar *)searchBar{ [searchBar setShowsCancelButton:NO animated:YES]; [searchBar resignFirstResponder]; tableViewObj.allowsSelection = YES; tableViewObj.scrollEnabled = YES; } - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar { [searchBar setShowsCancelButton:YES animated:YES]; // tableViewObj.allowsSelection = NO; tableViewObj.scrollEnabled = NO; } - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar { searchBar.text=@""; [searchBar setShowsCancelButton:NO animated:YES]; [searchBar resignFirstResponder]; tableViewObj.allowsSelection = YES; tableViewObj.scrollEnabled = YES; } - (void)viewDidAppear:(BOOL)animated { //[searchBarObj becomeFirstResponder]; //[super viewDidAppear:animated]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)dealloc { [searchBarObj release]; [tableViewObj release]; [super dealloc]; } @end
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท