简体   繁体   中英

Table search using UISearch controller for iOS 8 or later

因为不推荐使用searchDisplayController,所以任何人都可以帮助共享用于iOS 8或更高版本的表搜索功能的示例代码

Download the sample project given in this link. Hope that helps. It demonstrates the use of the UISearchController class based on tableView search.

I found this "UISearchDisplayController has been replaced with UISearchController". So let try UISearchController.

  1. you first create one textfield(as text) and one table view(as tableView) and their delegates in viewcontroller.h files
  2. and wirte this code in viewcontroller.m file

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
{

NSMutableArray *tableData;

NSMutableArray *tableArray;

NSMutableArray *dummyArray;
NSMutableArray *searchArray;
NSString *searchTextString;
}
- (void)viewDidLoad {
[super viewDidLoad];
//set the selector to the text field in order to change its value when edited
[self.text addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
//here you set up the methods to search array and reloading the tableview
[self setupData];
[self updateSearchArray];
[self.tableView reloadData];

// Do any additional setup after loading the view, typically from a nib.
_tableView.allowsSelection=NO;
self.text.delegate=self;
}
//setting up the data sourch for the mutable array
- (void) setupData {
dummyArray = [[NSMutableArray alloc] init];

[dummyArray addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"Values:", @"name" , @"image1.JPG", @"image" , @"dummy 1 description textview", @"description", nil]];
[dummyArray addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"Apple", @"name" , @"image1.JPG", @"image" , @"dummy 2 description textview", @"description", nil]];
[dummyArray addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"dummy", @"name" , @"image1.JPG", @"image" , @"dummy 3 description textview", @"description", nil]];
[dummyArray addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"Mango", @"name" , @"image1.JPG", @"image" , @"dummy 1 description textview", @"description", nil]];
[dummyArray addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"Brown", @"name" , @"image1.JPG", @"image" , @"dummy 1 description textview", @"description", nil]];
[dummyArray addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"jumps", @"name" , @"image1.JPG", @"image" , @"dummy 1 description textview", @"description", nil]];
[dummyArray addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"hello", @"name" , @"image1.JPG", @"image" , @"dummy 1 description textview", @"description", nil]];
[dummyArray addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"shahzaib", @"name" , @"image1.JPG", @"image" , @"dummy 1 description textview", @"description", nil]];

}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
NSLog(@"textFieldShouldBeginEditing");
textField.backgroundColor = [UIColor colorWithRed:220.0f/255.0f green:220.0f/255.0f blue:220.0f/255.0f alpha:1.0f];
return YES;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"touchesBegan:withEvent:");
[self.view endEditing:YES];
[super touchesBegan:touches withEvent:event];
[_tableView setHidden:YES];

}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];

if ([_text.text isEqualToString:@""] ||[_text.text isEqualToString:@" "])
{
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Warning" message:@"TextField is empty now!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];

}
else
{

    [dummyArray addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:_text.text, @"name" , @"image1.JPG", @"image" , @"dummy 1 description textview", @"description", nil]];
     [_tableView reloadData];
    searchTextString = textField.text;
    [self updateSearchArray];
}
return YES;
}

#pragma - mark TextField Delegate Methods
-(void)textFieldDidBeginEditing:(UITextField *)textField {
 [_tableView setHidden:NO];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [searchArray count];
}

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

static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if( cell == nil ){
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [[searchArray objectAtIndex:indexPath.row] objectForKey:@"name"];
return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

//[self performSegueWithIdentifier:@"DummyDetail" sender:[NSNumber numberWithInt:indexPath.row]];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
self.text.text=[[searchArray objectAtIndex:indexPath.row] objectForKey:@"name"];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - Search Methods

-(void)textFieldDidChange:(UITextField*)textField
{

searchTextString = textField.text;
[self updateSearchArray];
}
//update seach method where the textfield acts as seach bar
-(void)updateSearchArray
{
if (searchTextString.length != 0)
{
    _tableView.allowsSelection=YES;
    searchArray = [NSMutableArray array];
    for ( NSDictionary* item in dummyArray )
    {
        if ([[[item objectForKey:@"name"] lowercaseString] rangeOfString:[searchTextString lowercaseString]].location != NSNotFound)
        {
            [searchArray addObject:item];
        }
       else
       {
            //NSString *notfound;
            //notfound=@"not found";
            //[searchArray addObject:notfound];
        }
    }
}
else
{
    searchArray = dummyArray;
    _tableView.allowsSelection=NO;
}

[self.tableView reloadData];
}

its a complete search,i think this one help you, tell me if you need more help ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM