iOS開発の高度なビュー-UISEarchBar

4605 ワード

UIsearchBarは検索バーで、1つのテキストボックスといくつかのボタンで構成されており、ユーザーがテキストボックスに一部の内容を入力した後、プログラムは指定したルールに従って検索を実行することができます.
以下の例では、UItableViewとUIsearchBarを組み合わせて、簡単な本検索機能を実現しています.
   ViewController.m
//
//  ViewController.m
//  UITableViewSearchDemo
//
//  Created by Apple on 16/5/25.
//  Copyright © 2016  Apple. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

UITableView* tableView;
//          NSArray  。
NSArray * tableData;
//          NSArray  。
NSArray* searchData;
//       
bool isSearch;

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //         
    isSearch = NO;
    //          
    tableData = [NSArray arrayWithObjects:@"Java  ",
                 @"   Java EE      ",
                 @"Android  ",
                 @"Ajax  ",
                 @"HTML5/CSS3/JavaScript  ",
                 @"iOS  ",
                 @"XML  ",
                 @"  Java EE      "
                 @"Java     ",
                 @"Java    ",
                 @"  Java",
                 @"Objective-C  " ,
                 @"Ruby     ",
                 @"iOS    " , nil];
    
    //   UISearchBar  
    UISearchBar* searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 50, 100, 60)];
    
    //   Prompt  
    [searchBar setPrompt:@"    "];
    //              
    [searchBar setPlaceholder:@"       "];
    //   Cancel  
    searchBar.showsCancelButton = true;
    //     
    searchBar.delegate = self;
    
    //   UITableView
    tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
    //     
    tableView.dataSource = self;
    
    //   searchBar   table      
    tableView.tableHeaderView = searchBar;
    //   UITableView
    [self.view addSubview:tableView];
    
}

#pragma mark - UITableViewDataSource

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

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section
{
    //         
    if(isSearch)
    {
        //   searchData         
        return searchData.count;
    }
    else
    {
        //        tableData         
        return tableData.count;
    }
}

- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"----cellForRowAtIndexPath------");
    
    static NSString* cellId = @"cellId";
    //                 
    UITableViewCell* cell = [tableView
                             dequeueReusableCellWithIdentifier:cellId];
    //       nil
    if(!cell)
    {
        //      
        cell = [[UITableViewCell alloc] initWithStyle:
                UITableViewCellStyleDefault
                                      reuseIdentifier:cellId];
    }
    //                
    NSInteger rowNo = indexPath.row;
    //         
    if(isSearch)
    {
        //   searchData         
        cell.textLabel.text = [searchData objectAtIndex:rowNo];
    }
    else{
        //        tableData         
        cell.textLabel.text = [tableData objectAtIndex:rowNo];
    }
    return cell;
}

#pragma mark - UISearchBarDelegate

// UISearchBarDelegate     ,              
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    NSLog(@"----searchBarCancelButtonClicked------");
    //       
    isSearch = NO;
    [tableView reloadData];
}

// UISearchBarDelegate     ,                 
- (void)searchBar:(UISearchBar *)searchBar
    textDidChange:(NSString *)searchText
{
    NSLog(@"----textDidChange------");
    //   filterBySubstring:      
    [self filterBySubstring:searchText];
}

// UISearchBarDelegate     ,         Search        
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    NSLog(@"----searchBarSearchButtonClicked------");
    //   filterBySubstring:      
    [self filterBySubstring:searchBar.text];
    //           ,    
    [searchBar resignFirstResponder];
}

- (void) filterBySubstring:(NSString*) subStr
{
    NSLog(@"----filterBySubstring------");
    //        
    isSearch = YES;
    //       
    NSPredicate* pred = [NSPredicate predicateWithFormat:
                         @"SELF CONTAINS[c] %@" , subStr];
    //       NSArray
    searchData = [tableData filteredArrayUsingPredicate:pred];
    //            
    [tableView reloadData];
}



@end

効果図は以下の通りです.