iphone - 搜索不起作用

标签 iphone ios ios5 uisearchbar

我正在尝试进行搜索,但它对我来说并不完美。我需要什么,当我输入 B 时,我应该得到所有以 B 开头的单词。我还在想我哪里做错了。我对 ios 很陌生。

这是我的代码:- ContactViewController.h

 #import <UIKit/UIKit.h>

   @interface ContactViewController :  
  UIViewController<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>
  {
  UISearchBar* searchBar;
  IBOutlet UITableView* contactTableView;
  NSMutableArray *listOfItems;
  NSMutableArray *copyListOfItems;
  NSArray *content;
  NSArray *indices;
  NSArray* contacts;
  BOOL searching;
  BOOL letUserSelectRow;
   }
  -(void)btn_AddContact;
   @end

    ContactViewController.m

   #import "ContactViewController.h"
   #import "AddContactsViewController.h"
   #import "CustomCell.h"
   #import "DataGenerator.h"
   @interface ContactViewController ()
   @end

   @implementation ContactViewController
  - (void)viewDidLoad
    {
    [super viewDidLoad];
    const NSInteger searchBarHeight = 45;
    searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320,
    searchBarHeight)];
    [self.view addSubview:searchBar];
    searchBar.delegate = self;
    [self.view addSubview:searchBar];

    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:@"Refresh" 
    style:UIBarButtonItemStyleBordered target:self action:@selector(onAddContact)];
     self.navigationItem.rightBarButtonItem = addButton;
     UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont boldSystemFontOfSize:20.0];
    label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
    label.textAlignment = UITextAlignmentCenter;
    label.textColor = [UIColor whiteColor]; // change this color
    self.navigationItem.titleView = label;
    label.text = NSLocalizedString(@"All Contacts", @"");
    [label sizeToFit];
     content = [DataGenerator wordsFromLetters];
     indices = [[content valueForKey:@"headerTitle"] retain];
     }

   - (void)viewDidUnload
    {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
     } 

 - (BOOL)shouldAutorotateToInterfaceOrientation:  
           (UIInterfaceOrientation)interfaceOrientation
     {
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    }


     #pragma mark -
     #pragma mark Table view data source

  // Customize the number of sections in the table view.
  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
     return [content count];
      }

   // Customize the number of rows in the table view.
 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
    (NSInteger)section {
     return [[[content objectAtIndex:section] objectForKey:@"rowValues"] count] ;
    }


    // Customize the appearance of table view cells.
  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
      (NSIndexPath *)indexPath 
      {    
      static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView
         dequeueReusableCellWithIdentifier:CellIdentifier];
         if (cell == nil) {
         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
                                   reuseIdentifier:CellIdentifier];
       }
    if(searching) {
    cell.textLabel.text = [copyListOfItems objectAtIndex:indexPath.row];
    cell.detailTextLabel.text=[NSString stringWithFormat:@"%@ %@ %@",
     @"pauline.abraham@gmail.com", @" |",@"123456777"] ;
 }else {
     cell.textLabel.text = [[[content objectAtIndex:indexPath.section]
     objectForKey:@"rowValues"] 
                       objectAtIndex:indexPath.row];
  //cell.detailTextLabel.numberOfLines=2;
   //cell.detailTextLabel.lineBreakMode=NSLineBreakByWordWrapping;
  cell.detailTextLabel.text=[NSString stringWithFormat:@"%@ %@ %@",
     @"pauline.abraham@gmail.com", @" |",@"123456777"] ;
     }
   return cell;
     }

  - (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:
  (NSInteger)section {
return [[content objectAtIndex:section] objectForKey:@"headerTitle"];
    }
  - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return [content valueForKey:@"headerTitle"];
     }
   - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:
      (NSString *)title atIndex:(NSInteger)index {
       return [indices indexOfObject:title];
      }
    -(void)onAddContact
  {
   //    AddContactsViewController* add = [[AddContactsViewController alloc] 
           initWithNibName:@"AddContactsViewController" bundle:nil];
    //    [self.navigationController pushViewController:add animated:YES];
    }
   -(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
    {
    if(searching)
    return;
    searching = YES;
    letUserSelectRow = NO;
   [contactTableView setScrollEnabled:NO];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] 
    initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self 
    action:@selector(btn_DoneSearch)];
    }

   -(void)btn_DoneSearch
     {
  searchBar.text = @"";
  [searchBar resignFirstResponder];
  searching = NO;
  letUserSelectRow = YES;
  [contactTableView setScrollEnabled:YES];
  self.navigationItem.rightBarButtonItem = nil;
   [contactTableView reloadData];
  }

 -(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
//Remove all objects first.
[copyListOfItems removeAllObjects];
  if([searchText length] > 0){
      searching = YES;
      letUserSelectRow = YES;
      [contactTableView setScrollEnabled:YES];
      [self searchTableView];
      }else {
       searching = NO;
    letUserSelectRow = NO;
    [contactTableView setScrollEnabled:NO];
     }
    [contactTableView reloadData];
    }

   -(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
     {
    [self searchTableView];
     }

    -(void)searchTableView
     {
    NSString *searchText = searchBar.text;
    NSMutableArray *searchArray = [[NSMutableArray alloc] init];
    for (NSDictionary *dictionary in content)
    {
    NSArray *array = [dictionary objectForKey:@"rowValues"];
    [searchArray addObjectsFromArray:array];
    }
   for (NSString *sTemp in searchArray)
   {
    NSRange titleResultsRange = [sTemp rangeOfString:searchText 
   options:NSCaseInsensitiveSearch];
     if (titleResultsRange.length>0)
    {
        [copyListOfItems addObject:sTemp];
        NSLog(@"lenght : %d",titleResultsRange.length );
    }
}
searchArray = nil;
}
 @end

最佳答案

请参阅下面的代码,您还没有添加与搜索数据相关的行,将此条件也放入 UITableView 的以下 3 个委托(delegate)方法中

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

     if (searching)
       return 1;
     else
       return [content count];
}

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

     if (searching)
        return [copyListOfItems count];
     else
        return [[[content objectAtIndex:section] objectForKey:@"rowValues"] count] ;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    if(searching)
        return @"";
    else
       return [[content objectAtIndex:section] objectForKey:@"headerTitle"];

}

关于iphone - 搜索不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13914258/

相关文章:

iphone - 将 SQLite 数据库添加到 iPhone 应用程序

iphone - 继续不执行

iphone - ipad锁定: launch app on boot?

iphone - XCode:如何以编程方式获取用户的电子邮件?

ios - 从 JSON 端点检索数据

iphone - 发生当机事故时,TestFlight TFLog无法上传

ios - 如何使用 AFnetworking 发送 post 请求?

ios - 将 RGB 图像转换为 1 channel 图像(黑/白)

iphone - 如何从 UIImage 获取 URL

ios - -[GKPlayerInternal setAuthenticateHandler :]: unrecognized selector sent to instance