// // ViewController.h // searchBarControllerApp // // Created by Weerachai on 11/18/55 BE. // Copyright (c) 2555 Weerachai. All rights reserved. // #import <UIKit/UIKit.h> @interface ViewController : UIViewController <UISearchDisplayDelegate> @property (retain, nonatomic) IBOutlet UITableView *tableViewObj; @end
// // ViewController.m // searchBarControllerApp // // 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; } - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { if([searchString length] == 0) { [displayObject removeAllObjects]; [displayObject addObjectsFromArray:allObject]; } else { [displayObject removeAllObjects]; for(NSString * string in allObject) { NSRange r = [string rangeOfString:searchString options:NSCaseInsensitiveSearch]; if(r.location != NSNotFound) { [displayObject addObject:string]; } } } return YES; } - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar { searchBar.text=@""; [searchBar setShowsCancelButton:NO animated:YES]; [searchBar resignFirstResponder]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)dealloc { [_tableViewObj release]; [super dealloc]; } @end
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท