简体   繁体   中英

how to search content in search bar without press a button in iOS?

I want search content in my search bar without press search buttons means when i enter any text in search bar automatically search filter the content when i clear or cancel search then show my main content list here i implement code for search bar

-(void)SearchBarCode
{ 
    self.disableViewOverlay.backgroundColor=[UIColor blackColor];
    self.disableViewOverlay.alpha = 0;
    theSearchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(-61, 46, 378.0f, 50)];
    theSearchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    UIView *searchBarView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 46, 310.0, 44.0)];
    searchBarView.autoresizingMask = 0;
    theSearchBar.delegate =self;
    [searchBarView addSubview:theSearchBar];
    self.navigationItem.title=@"Insta SMS Collection";
    self.navigationItem.titleView=searchBarView;
    theSearchBar.placeholder = @"Search";
}

- (void)viewDidAppear:(BOOL)animated
{
    [self.theSearchBar resignFirstResponder];
    [super viewDidAppear:animated];
}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    theSearchBar.showsScopeBar = YES;
    [theSearchBar invalidateIntrinsicContentSize];
    [theSearchBar setShowsCancelButton:YES animated:YES];
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    [theSearchBar invalidateIntrinsicContentSize];
    [theSearchBar setShowsCancelButton:NO animated:YES];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    scrollView.scrollEnabled = TRUE;
    theSearchBar.text = @"";

    theSearchBar.text = @"";
    [theSearchBar setShowsCancelButton: NO animated: YES];
    [theSearchBar resignFirstResponder];
}

- (void)searchTableList
{
    NSString *searchString = theSearchBar.text;
    filteredContentList = [[NSMutableArray alloc]init];

    for (SMSCategory *cat in Arrayobject)
    {
        NSString *tempStr = cat.Name;
        NSComparisonResult result = [tempStr compare:searchString options: (NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, 
        [searchString length])];
        if (result == NSOrderedSame)
        {
            [filteredContentList addObject:cat];
        }
    }
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    scrollView.scrollEnabled = TRUE;

    [theSearchBar resignFirstResponder];
    [theSearchBar setShowsCancelButton: NO animated: YES];

    [self searchTableList];
    [self DynamicButton:filteredContentList];
    [self viewDidAppear:true];
}

.h File Code

@interface CategoryMainWindowViewController : UIViewController<UISearchBarDelegate,UITextViewDelegate>{
    NSMutableArray *Arrayobject;
    UIView *disableViewOverlay;
    UITableView *theTableView;
    UISearchBar *theSearchBar;
    NSMutableArray *filteredContentList;
    BOOL isSearching;
}

@property(retain) UIView *disableViewOverlay;
@property(retain) NSMutableArray *Arrayobject;
@property (nonatomic, retain) IBOutlet UITableView *theTableView;
@property (nonatomic, retain) IBOutlet UISearchBar *theSearchBar;
@property(nonatomic,retain) UIScrollView *scrollView;
@property(strong,nonatomic) NSString *databasePath;

-(void)searchBar:(UISearchBar *)searchBar activate:(BOOL) active;
-(void)DynamicButton:(NSMutableArray*)objectName;
-(NSMutableArray*)GetData;

@end

You can implement the delegate-method

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText;

This will be called every time the user enters a character. You then just have to search with the given string.

For example you could call the method, that is called when clicking search itself:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    [self searchBarSearchButtonClicked:searchBar];
}

assign Arrdata to ArrTemp in viewdidload mehod then use this method for searching if search field contains empty text then it will display main content who stored in ArrTemp previously.....

- (void)viewDidLoad
{
[super viewDidLoad];
ArrTemp = [[NSMutableArray alloc]initWithArray:Arrdata];

}

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    if (searchText.length >0) {
        NSPredicate *predicate = [NSPredicate
                                  predicateWithFormat:@"vName CONTAINS[cd] %@ OR vName LIKE[cd] %@",
                                  searchText,searchText];//
        NSArray *filteredArray = [Arrdata filteredArrayUsingPredicate:predicate];
        Arrdata = [[NSMutableArray alloc]initWithArray:filteredArray];

    }else{
        Arrdata = ArrTemp;
    }
    [tblview reloadData];

}

您可以在委托方法中使用searchBar.text ,searchBarTextDidEndEditing或searchBar:textDidChange:

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