UISearchBar ios 搜索栏

首先要在xib中拖动一个searchBar VC 也不用连方法的线。。。

UISearchBarDelegate,UISearchDisplayDelegate>

///搜索用的。

@property (strong, nonatomic) NSArray *arOriginal;

@property (strong, nonatomic) NSArray *arFiltered;

@property (readwrite, nonatomic) BOOL isSearching;

@property (weak, nonatomic) IBOutletUISearchBar *mySearchB;

 

 self.isSearching = NO;

    self.arOriginal = [NSArrayarrayWithObjects:@"Section0_Row1", @"Row1_Subrow1", @"Row1_Subrow2", @"Row1_Subrow3",nil];

 

    

 

    self.mySearchB.delegate = self;

 

 

 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

//    return [self.contents count];

    return (self.isSearching)?1:self.contents.count;

}

 

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

{

    return (self.isSearching)?self.arFiltered.count:[self.contents[section] count];

    

}

 

- (NSInteger)tableView:(SKSTableView *)tableView numberOfSubRowsAtIndexPath:(NSIndexPath *)indexPath

{

    if (self.isSearching) {

        return 0;

    }

    return [self.contents[indexPath.section][indexPath.row] count] - 1;

}

///这个都没有执行。。。

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

{

    if (self.isSearching) {///搜索到的结果。。。

        static NSString *CellIdentifier = @"UITableViewCell";

        

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        

        if (!cell)

            cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier];

        

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

        

        

        UIImageView *tempImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"DT_look.png"]];

        tempImageView.frame = CGRectMake(280, 18, 20, 16);

        [cell.contentView addSubview:tempImageView];

        

        return cell;

    }

    

    

    static NSString *CellIdentifier = @"SKSTableViewCell";

    

    SKSTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    

    if (!cell)

        cell = [[SKSTableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier];

    

    cell.textLabel.text = self.contents[indexPath.section][indexPath.row][0];

    NSLog(@"cell.textLabel.text==--------=%@",cell.textLabel.text);

    ///现在没有section 并且都要有下拉的。。。。所以不用判断。

    cell.isExpandable = YES;

    

    return cell;

}

#pragma mark =====真正执行的方法========

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

{

    static NSString *CellIdentifier = @"UITableViewCell";

    

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    

    if (!cell)

        cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier];

    

    cell.textLabel.text = [NSString stringWithFormat:@"%@", self.contents[indexPath.section][indexPath.row][indexPath.subRow]];

    NSLog(@"cell.textLabel.text===%@",cell.textLabel.text);

    cell.detailTextLabel.text = cell.textLabel.text;

    

    UIImageView *tempImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"DT_look.png"]];

    tempImageView.frame = CGRectMake(280, 18, 20, 16);

    [cell.contentView addSubview:tempImageView];

    return cell;

}

 

 

 

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    SKSTableViewCell *myCell = (SKSTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];

    NSLog(@"mycelll====%@",myCell.textLabel.text);

    self.isSearching = NO;

}

 

 

 

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {

    if(self.searchDisplayController.searchBar.text.length>0) {

        self.isSearching=YES;

        NSString *strSearchText = self.searchDisplayController.searchBar.text;

        

        

//        NSMutableArray *ar=[NSMutableArray array];

        // correctly working ! Thanx for watching video !

        

        

//        for (NSString *d in self.arOriginal) {

//            if([d rangeOfString:strSearchText].length>0) {

//                [ar addObject:d];

//            }

//        }

//        self.arFiltered=[NSArray arrayWithArray:ar];

        

        ///下面是用谓词逻辑的方法。。///[c]忽略大小写.[d]忽略重音。这两个可以一起写。

        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS  [cd] %@", strSearchText];

        self.arFiltered = [self.arOriginal filteredArrayUsingPredicate:predicate];

        

        

    } else {

        self.isSearching=NO;

    }

    returnYES;

}

 

- (void) searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller{

   ///每次搜索完的时候。

    self.isSearching = NO;

}

 

 

 

- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

 

@end

猜你喜欢

转载自zhangmingwei.iteye.com/blog/2024922