2016-06-25 132 views

回答

2

實現搜索功能,手動,像這樣 [

文本字段實現.h文件。

#import "ViewController.h" 

@interface ViewController() 

<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate> 
{ 
    NSMutableArray *arrOfColor; 
    NSMutableArray *searchArray; 
    NSString *searchTextString; 
    BOOL isFilter; 
} 

@property (strong, nonatomic) IBOutlet UITextField *searchTextField; 
@property (strong, nonatomic) IBOutlet UITableView *colorTableview; 

@end 

實現.m文件。

@implementation ViewController 

- (void)viewDidLoad 

{ 
    [super viewDidLoad]; 
    arrOfColor=[NSMutableArray arrayWithObjects:@"Red",@"Green",@"Blue",@"Gray",@"Black",@"White",@"Yellow",@"Brown",@"Pink",nil]; 
    [self.searchTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged]; 
} 

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
     return 1; 
} 

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

    if(isFilter) 
    { 
     return [searchArray count]; 
    } 
    else 
     return [arrOfColor count]; 
} 

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

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"]; 
    if(!cell) 
    { 
     cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; 
    } 
    if(isFilter) 
    { 
     cell.textLabel.text=[searchArray objectAtIndex:indexPath.row]; 
    } 
    else 
    { 
      cell.textLabel.text=[arrOfColor objectAtIndex:indexPath.row]; 
    } 

    return cell; 
} 

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if(isFilter) 
    { 
     _searchTextField.text=[searchArray objectAtIndex:indexPath.row]; 
    } 
    else 
    { 
     _searchTextField.text=[arrOfColor objectAtIndex:indexPath.row]; 

    } 
} 

-(void)textFieldDidChange:(UITextField *)textField 
{ 
    searchTextString=textField.text; 
    [self updateSearchArray:searchTextString]; 
} 

-(void)updateSearchArray:(NSString *)searchText 
{ 
    if(searchText.length==0) 
    { 
     isFilter=NO; 
    } 
    else 
    { 
     isFilter=YES; 
     searchArray=[[NSMutableArray alloc]init]; 
     for(NSString *string in arrOfColor){ 

      NSRange stringRange=[string rangeOfString:searchText options:NSCaseInsensitiveSearch]; 
      if(stringRange.location !=NSNotFound){ 

       [searchArray addObject:string]; 
      } 
     } 
     [self.colorTableview reloadData];} 
    } 

-(BOOL)textFieldShouldReturn:(UITextField *)textField 
{ 
    [textField resignFirstResponder]; 
    return YES; 
} 
@end 
+0

謝謝,但我使用Searchbar而不是textfield bro。 –