2012-12-17 61 views
0

我正在嘗試搜索,但它不能完全爲我工作。當我輸入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:@"%@ %@ %@", 
    @"[email protected]", @" |",@"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:@"%@ %@ %@", 
    @"[email protected]", @" |",@"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 
+0

你正在添加searchBar 2次[self.view addSubview:searchBar]; – Rajneesh071

回答

1

看到婁代碼你也不會增加找遍把數據的UITableView

- (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"]; 

    } 
波紋管3的委託方法這個條件相關的行

看到教程與包含演示從這波紋管鏈接

uitableview-searching-table-view

我希望這對你有幫助...

+0

Thanx爲寶貴的答案。但是現在當我將B輸入serachbar時,我沒有任何意義的空白屏幕。我的意思是我可以在右側看到一個空白屏幕和索引。 –

+0

嘿夥計首先從這個鏈接http://www.iphonesdkarticles.com/2009/01/uitableview-searching-table-view.html下載演示,並按照這個教程,如果有任何幫助,然後發表評論兄弟在這裏有一些錯誤的代碼夥計所以... –

+1

thanx .....現在它爲我工作。其實我忘了初始化copyListOfItems。即\t copyListOfItems = [[NSMutableArray alloc] init]; –