zoukankan      html  css  js  c++  java
  • UItableView和UISearchBar实现简单表格搜索

    UItableView和UISearchBar实现简单表格搜索

    /*********************************************************************

     文件名称 :SingletonDemoTest.m

           :好岸园技

              : http://www.hopean.com

     时间 : 12-11-7

     文件描述 : 

      

         : 好岸园技

              http://www.hopean.com

     *********************************************************************/


    - (void)viewDidLoad

    {

        [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

        

        UITableView *tmpTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];

        tmpTableView.delegate = self;

        tmpTableView.dataSource = self;

        self.tableView = tmpTableView;

        [self.view addSubview:tmpTableView];

        [tmpTableView release];

        

        

        self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 44)];

        self.searchBar.delegate = self;

        self.tableView.tableHeaderView = self.searchBar;

        

        

        NSMutableArray *tmpArr = [[NSMutableArray alloc] initWithObjects:@"aaaaa",@"bbbbb",@"ccccc",@"dddd",@"eeee",@"aaaaa",@"bbbbb",@"ccccc",@"dddd",@"eeee",@"aaaaa",@"bbbbb",@"ccccc",@"dddd",@"eeee",@"aaaaa",@"bbbbb",@"ccccc",@"dddd",@"eeee",@"aaaaa",@"bbbbb",@"ccccc",@"dddd",@"eeee",@"aaaaa",@"bbbbb",@"ccccc",@"dddd",@"eeee",@"aaaaa",@"bbbbb",@"ccccc",@"dddd",@"eeee",@"aaaaa",@"bbbbb",@"ccccc",@"dddd",@"eeee", nil];

        self.contentArray = tmpArr;

        [tmpArr release];

        

    }





    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

        return [self.contentArray count];

    }


    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    {

        //NSLog(@"RootView:cellForRowAtIndexPath");

        static NSString *CellIdentifier = @"Cell";

        

        UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (cell == nil) {

            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

        }

        

        cell.textLabel.text = [self.contentArray objectAtIndex:indexPath.row];

        //[contact release]; 不能释放

        

        return cell;

    }



    #pragma mark - Searching



    - (void)updateSearchString:(NSString*)aSearchString

    {

        //[aSearchString release];

        //aSearchString = [[NSString alloc]initWithString:aSearchString];

        

        //[[DataController instance] filterContactsWithLastName:searchString];

        NSMutableArray *array = [[NSMutableArray alloc] init];

        

        for(NSString *str in self.contentArray)

        {

            

            if ([str rangeOfString:aSearchString options:NSCaseInsensitiveSearch].location != NSNotFound) {

                [array addObject:str];

            }

        }

        

    //    if ([array count] == [self.contentArray count]) {

    //        

    //    }

        self.contentArray = [[NSMutableArray alloc] initWithArray:array];

        //array;

        

        [array release];

        [self.tableView reloadData];

    }


    - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar

    {

        [self.searchBar setShowsCancelButton:YES animated:YES];

        self.tableView.allowsSelection = NO;

        self.tableView.scrollEnabled = NO;

    }


    - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar

    {

        [self.searchBar setShowsCancelButton:NO animated:YES];

        [self.searchBar resignFirstResponder];

        self.tableView.allowsSelection = YES;

        self.tableView.scrollEnabled = YES;

        self.searchBar.text=@"";

        [self updateSearchString:searchBar.text];

        self.contentArray = [[NSMutableArray alloc] initWithObjects:@"aaaaa",@"bbbbb",@"ccccc",@"dddd",@"eeee",@"aaaaa",@"bbbbb",@"ccccc",@"dddd",@"eeee",@"aaaaa",@"bbbbb",@"ccccc",@"dddd",@"eeee",@"aaaaa",@"bbbbb",@"ccccc",@"dddd",@"eeee",@"aaaaa",@"bbbbb",@"ccccc",@"dddd",@"eeee",@"aaaaa",@"bbbbb",@"ccccc",@"dddd",@"eeee",@"aaaaa",@"bbbbb",@"ccccc",@"dddd",@"eeee",@"aaaaa",@"bbbbb",@"ccccc",@"dddd",@"eeee", nil];

        [self.tableView reloadData];

    }


    - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar

    {

        self.tableView.allowsSelection = YES;

        self.tableView.scrollEnabled = YES;

        [self updateSearchString:self.searchBar.text];

        [self.searchBar resignFirstResponder];   //隐藏输入键盘

    }


  • 相关阅读:
    异常处理的讨论 CQ
    看看这个Lock可不可靠 CQ
    Smart Client Software Factory(SCSF) 之起步 CQ
    建设高性能网站
    关系数据库还是NoSQL数据库
    另类递归
    cacti监控redis状态
    NoSQL书籍大全
    如果判断function的调用者?
    Error while creating db diagram:Cannot insert NULL into column diagram_id
  • 原文地址:https://www.cnblogs.com/hopeanCom/p/2789565.html
Copyright © 2011-2022 走看看