码迷,mamicode.com
首页 > 移动开发 > 详细

iOS.UIKit.14.UITableView -- UISearchBar

时间:2014-06-06 14:45:32      阅读:326      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   java   

1、案例介绍:一个具备搜索功能的表视图,如图01,02,03

bubuko.com,布布扣图01bubuko.com,布布扣图02bubuko.com,布布扣图03

2、Main.storyboard,如图04

bubuko.com,布布扣图04

3、.h

bubuko.com,布布扣
#import <UIKit/UIKit.h>

@interface CQ23ViewController : UITableViewController<UISearchBarDelegate, UISearchDisplayDelegate>
// 搜索栏
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
// 所有队伍集合
@property (nonatomic, strong) NSArray *listTeams;
// 查询后的队伍集合
@property (nonatomic, strong) NSMutableArray *listFilterTeams;

// 按条件查询,加载查询出的内容,给listFilterTeams赋值
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSUInteger)scope;

@end
bubuko.com,布布扣

4、.m

 

bubuko.com,布布扣
#import "CQ23ViewController.h"

@interface CQ23ViewController ()

@end

@implementation CQ23ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //设定搜索栏ScopeBar隐藏
    [self.searchBar setShowsScopeBar:NO];
    [self.searchBar sizeToFit];
    
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *plistPath = [bundle pathForResource:@"team"
                                           ofType:@"plist"];
    //获取属性列表文件中的全部数据
    self.listTeams = [[NSArray alloc] initWithContentsOfFile:plistPath];
    
    //初次进入查询所有数据
    [self filterContentForSearchText:@"" scope:-1];
}

- (void)viewDidUnload
{
    [self setSearchBar:nil];
    [super viewDidUnload];
}

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


#pragma mark Content Filtering
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSUInteger)scope;
{
    
    if([searchText length]==0)
    {
        //查询所有
        self.listFilterTeams = [NSMutableArray arrayWithArray:self.listTeams];
        return;
    }
    
    NSPredicate *scopePredicate;
    NSArray *tempArray ;
    
    switch (scope) {
        case 0: //英文
            scopePredicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@",searchText];
            tempArray =[self.listTeams filteredArrayUsingPredicate:scopePredicate];
            self.listFilterTeams = [NSMutableArray arrayWithArray:tempArray];
            
            break;
        case 1:
            scopePredicate = [NSPredicate predicateWithFormat:@"SELF.image contains[c] %@",searchText];
            tempArray =[self.listTeams filteredArrayUsingPredicate:scopePredicate];
            self.listFilterTeams = [NSMutableArray arrayWithArray:tempArray];
            
            break;
        default:
            //查询所有
            self.listFilterTeams = [NSMutableArray arrayWithArray:self.listTeams];
            break;
    }
}

#pragma mark --UITableViewDataSource 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.listFilterTeams count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    
    
    NSUInteger row = [indexPath row];
    NSDictionary *rowDict = [self.listFilterTeams objectAtIndex:row];
    cell.textLabel.text =  [rowDict objectForKey:@"name"];
    cell.detailTextLabel.text = [rowDict objectForKey:@"image"];
    
    NSString *imagePath = [rowDict objectForKey:@"image"];
    imagePath = [imagePath stringByAppendingString:@".png"];
    cell.imageView.image = [UIImage imageNamed:imagePath];
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}

#pragma mark --UISearchBarDelegate 协议方法
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    // 当点击取消按钮,查询所有
    [self filterContentForSearchText:@"" scope:-1];
}


#pragma mark - UISearchDisplayController Delegate Methods
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    // 当文本内容发生改变时候,向表视图数据源发出重新加载消息
    [self filterContentForSearchText:searchString scope:self.searchBar.selectedScopeButtonIndex];
    //YES情况下表视图可以重新加载
    return YES;
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
    // 当Scope Bar选择发生变化时候,向表视图数据源发出重新加载消息
    [self filterContentForSearchText:self.searchBar.text scope:searchOption];
    // YES情况下表视图可以重新加载
    return YES;
}
@end
bubuko.com,布布扣

 

iOS.UIKit.14.UITableView -- UISearchBar,布布扣,bubuko.com

iOS.UIKit.14.UITableView -- UISearchBar

标签:c   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/cqchen/p/3766566.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!