2013-06-22 4 views
5

जैसा कि मैंने एक आवश्यकता के साथ मुलाकात की जिसमें मेरे पास UISearchBar जैसा था।UISearchBar जैसा कि iMac खोजक खोज कार्यक्षमता

चरण: 1 (SearchBar की प्रारंभिक देखो)

enter image description here

चरण: 2 (उपयोगकर्ता प्रकार स्ट्रिंग और तत्काल खोजें)

enter image description here

कदम: 3 (खोज सूची में उनमें से किसी का चयन करना)

enter image description here

कदम: 4

(SearchBar पर बटन जोड़ने से)

enter image description here

कदम: 5 (अंत में बटन पर कार्रवाई)

enter image description here

यह जारी हो सकता है। मेरा कहना है कि वह पाठ को और अधिक दर्ज कर सकता है लेकिन कार्यक्षमता एक जैसी होनी चाहिए।

क्या कोई मेरी मदद कर सकता है? कृपया, मुझे ज़रूरत है।

अद्यतन

आप अपने आईमैक खोजक खोजें

यह मैं क्या कोशिश की है है यह एक ही कार्यक्षमता देख सकते हैं।

#import "ViewController.h" 
#import "customPopOverController.h" 
#import <QuartzCore/QuartzCore.h> 
@interface ViewController() 
@property (strong, nonatomic) UIView *searchView; 
@property (strong, nonatomic) UITableView *sampleView; 
@property (strong, nonatomic) NSMutableArray *arrayForListing; 
@property (strong, nonatomic) UITextField *txtFieldSearch; 
@end 

@implementation ViewController 
@synthesize searchView = _searchView; 
@synthesize customPopOverController = _customPopOverController; 
@synthesize txtFieldSearch = _txtFieldSearch; 
@synthesize sampleView = _sampleView; 
@synthesize arrayForListing = _arrayForListing; 
@synthesize btnforExtra = _btnforExtra; 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.arrayForListing = [[NSMutableArray alloc]init]; 
    [self loadTheSearchView]; 
    [self applyUIStyle]; 
} 
- (void)loadTheSearchView 
{ 
    self.searchView = [[UIView alloc]initWithFrame:CGRectMake(20, 100, 280, 44)]; 
    [self.searchView setBackgroundColor:[UIColor whiteColor]]; 
    [self.view addSubview:self.searchView]; 
    [self placeTheTextView]; 
} 
- (void)placeTheTextView 
{ 
    self.txtFieldSearch = [[UITextField alloc]initWithFrame:CGRectMake(25, 9, 230, 30)]; 
    [self.txtFieldSearch setDelegate:self]; 
    [self.searchView addSubview:self.txtFieldSearch]; 
} 
- (void)applyUIStyle 
{ 
    self.searchView.layer.cornerRadius = 20.0f; 
} 
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField 
{ 
    return YES; 
} 
- (void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
} 
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField 
{ 
    return YES; 
} 
- (void)textFieldDidEndEditing:(UITextField *)textField 
{ 

} 
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{ 
    if(textField.text.length!=0) 
    { 
     [self.arrayForListing removeAllObjects]; 
     [self.arrayForListing addObject:[NSString stringWithFormat:@"File Contains %@",textField.text]]; 

     [self callThePop]; 
    } 
    return YES; 
} 
- (void)callThePop 
{ 
    UIViewController *contentViewController = [[UIViewController alloc] init]; 
    contentViewController.contentSizeForViewInPopover = CGSizeMake(self.searchView.frame.size.width, 50); 
    self.sampleView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, contentViewController.contentSizeForViewInPopover.width, contentViewController.contentSizeForViewInPopover.height)]; 
    [self.sampleView setDelegate:self]; 
    [self.sampleView setDataSource:self]; 
    [self.sampleView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLineEtched]; 
    [self.sampleView setBackgroundColor:[UIColor clearColor]]; 
    [contentViewController.view addSubview:self.sampleView]; 

    self.customPopOverController = [[customPopOverController alloc]initWithContentViewController:contentViewController]; 
    self.customPopOverController.delegate = self; 

    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(callYourMethod:)]; 
    swipeRight.direction = UISwipeGestureRecognizerDirectionRight; 
    [self.sampleView addGestureRecognizer:swipeRight]; 

    [self.customPopOverController presentPopoverFromRect:self.searchView.frame inView:self.view permittedArrowDirections:(UIPopoverArrowDirectionUp|UIPopoverArrowDirectionDown| UIPopoverArrowDirectionLeft|UIPopoverArrowDirectionRight) animated:YES]; 
} 
- (BOOL)textFieldShouldClear:(UITextField *)textField 
{ 
    return YES; 
} 
- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{ 
    [textField resignFirstResponder]; 
    return YES; 
} 
- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
} 
#pragma mark - Table View Delegate Methods 
#pragma mark 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return self.arrayForListing.count; 
} 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifer = @"Cell"; 
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifer]; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifer]; 
    } 
    cell.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 
    [cell.textLabel setTextAlignment:UITextAlignmentCenter]; 
    [cell.textLabel setNumberOfLines:0]; 
    [cell.textLabel setLineBreakMode:UILineBreakModeCharacterWrap]; 
    cell.textLabel.text = [self.arrayForListing objectAtIndex:indexPath.row]; 
    [cell.textLabel setFont:[UIFont fontWithName:@"TrebuchetMS" size:14]]; 
    cell.textLabel.textColor = [UIColor whiteColor]; 
    return cell; 
} 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (self.customPopOverController) 
    { 
     [self.customPopOverController dismissPopoverAnimated:YES]; 
    } 
    UITableViewCell * cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath]; 
    if (self.txtFieldSearch.text.length == 0) 
    { 
     self.txtFieldSearch.text = cell.textLabel.text; 
    } 
    else 
    { 
     self.btnforExtra = [[UIButton alloc]initWithFrame:CGRectMake(10+(buttonsCount*45), 8, 45, 25)]; 
     [self.btnforExtra setBackgroundColor:[UIColor colorWithRed:0.503 green:0.641 blue:0.794 alpha:1.000]]; 
     [self.btnforExtra setTitle:self.txtFieldSearch.text forState:UIControlStateNormal]; 
     [self.btnforExtra setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
     [self.btnforExtra.layer setCornerRadius:8.0f]; 
     [self.txtFieldSearch setFrame:CGRectMake(self.btnforExtra.frame.origin.x+self.btnforExtra.frame.size.width+10, 9, 230, 30)]; 
     self.txtFieldSearch.text = @""; 
     [self.searchView addSubview:self.btnforExtra]; 
     buttonsCount++; 
    } 
} 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (tableView.editing == UITableViewCellEditingStyleDelete) 
    { 
     [tableView beginUpdates]; 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft]; 
     [self.arrayForListing removeObjectAtIndex:indexPath.row]; 
     [tableView endUpdates]; 
    } 
} 
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"IndexPath.row == %i", indexPath.row); 
} 
#pragma mark - custom PopOver Delegate Methods 
#pragma mark 
- (BOOL)popoverControllerShouldDismissPopover:(customPopOverController *)thePopoverController 
{ 
    return YES; 
} 
- (void)popoverControllerDidDismissPopover:(customPopOverController *)thePopoverController 
{ 
    self.customPopOverController = nil; 
} 

@end 
+4

जिस तरह से आप की व्याख्या की अपने प्रश्न बहुत बढ़िया है ..... –

+1

यह लूगा यदि आप अपना फ़ॉन्ट आकार घटा सकते हैं तो बहुत अच्छा। – Bhavin

+1

जैसा कि आपको कोई विशेष समस्या नहीं दिखती है, यहां सलाह का एक टुकड़ा है: पॉपओवर के साथ एक बटन अवधि जैसे अलग-अलग वर्ग बनाएं, खोज क्षेत्र के लिए एक और पॉपओवर इत्यादि। समस्या को कार्यों में विभाजित करना और कुछ पर बिंदु वे सभी एक साथ काम करेंगे। –

उत्तर

0

अस्वीकरण: इस समाधान, गहरा त्रुटिपूर्ण है शायद कई मायनों में। यह पहला समाधान है जिसके साथ मैं आया था और मैंने इस पर बहुत कम अनुकूलन किया है (उर्फ कोई नहीं)।

तो, मैं ऊब गया था, और मैं ऐसा कुछ लेकर आया जो मुझे लगता है कि बिल फिट बैठता है। ध्यान रखें कि यह केवल उस चीज का प्रदर्शन करता है जो आप पूछ रहे हैं (और यह उस पर कुछ हद तक खराब है), यह वास्तविक खोज कार्यक्षमता को संभाल नहीं करता है क्योंकि मुझे यकीन नहीं है कि आप क्या खोज रहे हैं। इसमें कुछ स्थिर मान भी प्रोग्राम किए गए हैं (जैसे फाइल प्रकार सरणी, और मैंने व्यू कंट्रोलर को UINavigationController में गिरा दिया और उस पर आधारित कुछ स्थिर आकार) जिन्हें आप बदलना चाहते हैं। मैं WYPopoverController का उपयोग करता हूं, जो यहां पाया जा सकता है: WYPopoverController। मुझे बताएं कि यह आपके लिए कैसे काम करता है- अगर यह नरक की आलोचना करने के लिए स्वतंत्र महसूस करें।

ViewController.h

@interface ViewController : UIViewController <UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource> 
{ 
} 


ViewController।मीटर

#import "ViewController.h" 
#import "WYPopoverController.h" 
#import "SearchButton.h" 
#import <QuartzCore/QuartzCore.h> 

@interface ViewController() <WYPopoverControllerDelegate> 
{ 
    UITextField *searchField; 

    UIView *searchesView; 
    UIScrollView *scrollView; 
    WYPopoverController *newSearchController; 
    WYPopoverController *existingSearchController; 
    NSMutableArray *buttonsArray; 
    SearchButton *activeButton; 
    NSArray *kindsArray; 
    NSMutableArray *matchedKinds; 

    // using UITableViewController instead of just UITableView so I can set preferredContentSize on the controller 
    UITableViewController *searchTable; 
    UITableViewController *existingTable; 
} 

@end 

@implementation ViewController 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 

    searchField = [[UITextField alloc] initWithFrame:CGRectMake(20, 75, 280, 30)]; 
    searchField.borderStyle = UITextBorderStyleRoundedRect; 
    searchField.autocorrectionType = UITextAutocorrectionTypeNo; 
    searchField.delegate = self; 
    [self.view addSubview:searchField]; 

    searchesView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, searchField.frame.size.height)]; 
    scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 0, searchField.frame.size.height)]; 

    [scrollView addSubview:searchesView]; 

    searchField.leftView = scrollView; 
    searchField.leftViewMode = UITextFieldViewModeAlways; 
    [searchField becomeFirstResponder]; 

    kindsArray = [NSArray arrayWithObjects:@"Audio", @"Video", @"Other", @"Text", nil]; 
    matchedKinds = [[NSMutableArray alloc] init]; 
    buttonsArray = [[NSMutableArray alloc] init]; 

    [searchField addTarget:self 
        action:@selector(textFieldDidChange:) 
      forControlEvents:UIControlEventEditingChanged]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 


#pragma mark - Text Field Delegate 

- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{ 
    [newSearchController dismissPopoverAnimated:YES completion:^{ 
     [self popoverControllerDidDismissPopover:newSearchController]; 
    }]; 

    return YES; 
} 

// not technically part of the TF delegate, but it fits better here 
- (void)textFieldDidChange:(UITextField *)tf 
{ 
    if(tf.text.length > 0) 
    { 
     if(newSearchController == nil) 
     { 
      searchTable = [[UITableViewController alloc] init]; 
      searchTable.tableView.delegate = self; 
      searchTable.tableView.dataSource = self; 
      searchTable.preferredContentSize = CGSizeMake(320, 136); 

      newSearchController = [[WYPopoverController alloc] initWithContentViewController:searchTable]; 
      newSearchController.delegate = self; 
      newSearchController.popoverLayoutMargins = UIEdgeInsetsMake(10, 10, 10, 10); 
      newSearchController.theme.arrowHeight = 5; 
      searchTable.tableView.tag = 1; 

      [newSearchController presentPopoverFromRect:searchField.bounds 
               inView:searchField 
           permittedArrowDirections:WYPopoverArrowDirectionUp 
               animated:YES 
               options:WYPopoverAnimationOptionFadeWithScale]; 
     } 

     [matchedKinds removeAllObjects]; 
     for(NSString *type in kindsArray) 
     { 
      NSRange foundRange = [[type lowercaseString] rangeOfString:[tf.text lowercaseString]]; 
      if(foundRange.location != NSNotFound) 
      { 
       // Found a match! 
       [matchedKinds addObject:type]; 
      } 
     } 

     [searchTable.tableView reloadData]; 
    } 
    else 
    { 
     [newSearchController dismissPopoverAnimated:YES completion:^{ 
      [self popoverControllerDidDismissPopover:newSearchController]; 
     }]; 
    } 
} 



#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    if(tableView.tag == 1) 
     return 2; 

    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if(tableView.tag == 1) 
    { 
     if(section == 0) 
      return 1; 

     return [matchedKinds count]; 
    } 

    return 3; 
} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    if(tableView.tag == 1) 
    { 
     if(section == 0) 
      return @"Filenames"; 
     else 
      return @"Kinds"; 
    } 

    return nil; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return 30.0f; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                reuseIdentifier:@"reuser"]; 

    // Configure the cell... 
    if(tableView.tag == 1) 
    { 
     if(indexPath.section == 0) 
     { 
      cell.textLabel.text = [NSString stringWithFormat:@"Name matches: %@", searchField.text]; 

     } 
     else 
     { 
      cell.textLabel.text = [matchedKinds objectAtIndex:indexPath.row]; 
     } 
    } 
    else 
    { 
     if(indexPath.row == 0) 
     { 
      switch (tableView.tag) { 
       case 2: 
        cell.textLabel.text = @"Filename"; 
        break; 

       default: 
        cell.textLabel.text = @"Kind"; 
        break; 
      } 
     } 
     else if(indexPath.row == 1) 
      cell.textLabel.text = @"Everything"; 
     else 
      cell.textLabel.text = @"<Delete>"; 

    } 


    return cell; 
} 


#pragma mark - Table view delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if(tableView.tag == 1) // new search table tapped 
    { 
     SearchButton *searchedButton = [SearchButton buttonWithType:UIButtonTypeSystem]; 

     if(indexPath.section == 0) 
     { 
      searchedButton.type = ButtonTypeName; 
      searchedButton.searchedString = searchField.text; 
     } 
     else 
     { 
      searchedButton.type = ButtonTypeKind; 
      searchedButton.searchedString = [matchedKinds objectAtIndex:indexPath.row]; 
     } 

     searchedButton.defaulted = YES; 
     searchedButton.titleLabel.font = [UIFont systemFontOfSize:14.0f]; 
     [searchedButton setTitleColor:[UIColor darkTextColor] 
          forState:UIControlStateNormal]; 
     [searchedButton addTarget:self 
          action:@selector(buttonTapped:) 
       forControlEvents:UIControlEventTouchUpInside]; 
     [searchedButton setBackgroundColor:[UIColor colorWithWhite:0.9f alpha:1.0f]]; 
     searchedButton.layer.cornerRadius = 5.0f; 
     searchedButton.clipsToBounds = YES; 
     [buttonsArray addObject:searchedButton]; 
     activeButton = searchedButton; 
     searchField.text = @""; 

     [self updateButtonsFromCreation:YES]; 


     [newSearchController dismissPopoverAnimated:YES completion:^{ 
      [self popoverControllerDidDismissPopover:newSearchController]; 
     }]; 
    } 
    else // text field of an existing search 
    { 
     switch (indexPath.row) { 
      case 0: 
       activeButton.defaulted = YES; 
       break; 

      case 1: 
       activeButton.defaulted = NO; 
       break; 

      default: 
       [buttonsArray removeObject:activeButton]; 
       break; 
     } 

     [self updateButtonsFromCreation:NO]; 
     [existingSearchController dismissPopoverAnimated:YES completion:^{ 
      [self popoverControllerDidDismissPopover:existingSearchController]; 
     }]; 


    } 
} 


#pragma mark - Popover delegate 

- (void)popoverControllerDidDismissPopover:(WYPopoverController *)controller 
{ 
    if(controller == newSearchController) 
    { 
     newSearchController = nil; 
    } 
    else 
    { 
     existingSearchController = nil; 
    } 
} 



#pragma mark - Other functions 

- (void)updateButtonsFromCreation:(BOOL)creation 
{ 
    [[searchesView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)]; 
    float widthTotal = 0; 
    for(SearchButton *button in buttonsArray) 
    { 
     NSString *buttonText; 

     if(button.defaulted) 
     { 
      if(button.type == ButtonTypeName) 
       buttonText = @"Name: "; 
      else 
       buttonText = @"Kind: "; 
     } 
     else 
      buttonText = @"Any: "; 

     buttonText = [buttonText stringByAppendingString:button.searchedString]; 

     [button setTitle:buttonText forState:UIControlStateNormal]; 
     CGSize buttonSize = [buttonText sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0f]}]; 
     button.frame = CGRectMake(widthTotal + 2, 2, buttonSize.width + 4, searchField.frame.size.height - 4); 
     widthTotal += button.frame.size.width + 2; 

     [searchesView addSubview:button]; 
    } 

    searchesView.frame = CGRectMake(0, 0, widthTotal, searchesView.frame.size.height); 
    scrollView.frame = CGRectMake(0, 0, ((widthTotal > 200) ? 200 : widthTotal), scrollView.frame.size.height); 
    scrollView.contentSize = CGSizeMake(widthTotal, scrollView.frame.size.height); 

    if(creation) 
    { 
     scrollView.contentOffset = CGPointMake(activeButton.frame.origin.x, 0); 
    } 
} 


- (void)buttonTapped:(SearchButton *)sender 
{ 
    activeButton = sender; 
    existingTable = [[UITableViewController alloc] init]; 
    existingTable.tableView.delegate = self; 
    existingTable.tableView.dataSource = self; 
    existingTable.preferredContentSize = CGSizeMake(160, 90); 

    existingSearchController = [[WYPopoverController alloc] initWithContentViewController:existingTable]; 
    existingSearchController.delegate = self; 
    existingSearchController.popoverLayoutMargins = UIEdgeInsetsMake(10, 10, 10, 10); 
    existingSearchController.theme.arrowHeight = 5; 
    existingTable.tableView.tag = sender.type; 

    [existingSearchController presentPopoverFromRect:sender.frame 
               inView:scrollView 
          permittedArrowDirections:WYPopoverArrowDirectionUp 
              animated:YES 
              options:WYPopoverAnimationOptionFadeWithScale]; 
} 


@end 


SearchButton.h

typedef enum {ButtonTypeName = 2, 
       ButtonTypeKind = 3} ButtonType; 

@interface SearchButton : UIButton 

@property (nonatomic) ButtonType type; 
@property (nonatomic) BOOL defaulted; 
@property (nonatomic, retain) NSString *searchedString; 

@end 


SearchButton.m

no changes to default generated