IOS 添加索引

 

//  ViewController.m

//  6.2(3) 简单表示图

//

//  Created by 陈庆龄 on 15/10/22.

//  Copyright © 2015年 陈庆龄. All rights reserved.

//

#import "ViewController.h"

#import "CustomCell.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;

@property (nonatomic,strong)NSArray *listTeam;

@property (nonatomic,strong)NSMutableArray *listFilterTeams;

//从plist文件中读取出来的数据

@property (nonatomic,strong)NSDictionary *dictData;

//小组名集合

@property (nonatomic,strong)NSArray *listGroupname;

-(void)filterContentForSearchText:(NSString* )searchText scope:(NSUInteger)scope;

@end

@implementation ViewController

- (void)viewDidLoad

{

    [super viewDidLoad];

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

    

    //设置搜索栏委托对象为当前视图控制器

    self.searchBar.delegate =self;

    

    

    //设定搜索栏ScopeBar为隐藏

    self.searchBar.showsScopeBar = NO;

    [self.searchBar sizeToFit];

    

    NSBundle *bundle= [NSBundle mainBundle];

    NSString * plistPath = [bundle pathForResource:@"team_dictionary" ofType:@"plist"];

    

    //获取属性列表的全部数据

    self.dictData = [[NSDictionary alloc]initWithContentsOfFile:plistPath];

    

    NSArray *templist= [self.dictData allKeys];

    //对KEY进行排序

    self.listGroupname = [templist sortedArrayUsingSelector:@selector(compare:)];

    

    

   }

    

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

//过滤结果集方法

-(void)filterContentForSearchText:(NSString *)searchText scope:(NSUInteger)scope

    {

        if ([searchText length]==0)

        {

            //查询所有

            self.listFilterTeams = [NSMutableArray arrayWithArray:self.listTeam];

            return;

        }

        NSPredicate *scopePredicate;

        NSArray *temArray;

        switch (scope)

        {

            case 0://按照英文名进行条件查询,其中image字段是球队的英文名

                scopePredicate = [NSPredicate predicateWithFormat:@"SELF.image contains[c]%@",searchText];

                temArray = [self.listTeam filteredArrayUsingPredicate:scopePredicate];

                self.listFilterTeams = [NSMutableArray arrayWithArray:temArray];

                

                break;

            case 1 ://按照中文名进行条件查询,其中name字段是球队的英文名

                scopePredicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c]%@",searchText];

                temArray = [self.listTeam filteredArrayUsingPredicate:scopePredicate];

                self.listFilterTeams = [NSMutableArray arrayWithArray:temArray];

                break;

                

                

            default:

                //查询所有

                self.listFilterTeams = [NSMutableArray arrayWithArray:self.listTeam];

                

                break;

        }

        

//        //将结果放到属性listFilterTeam中

//        NSMutableArray *array = [NSMutableArray arrayWithObjects:@"Bill",@"Ben",@"Chris",@"Melissa" ,nil];

//        NSPredicate *bPredicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c]'b'"];

//        NSArray *beginWithB = [array filteredArrayUsingPredicate:bPredicate];  //begainwithB 包含{@“Bill”,@“Ben”}

//        

//        

//        NSPredicate *sPredicate = [NSPredicate predicateWithFormat:@"SELF  contains[c]'s'"];

//        [array filteredArrayUsingPredicate:sPredicate];

//        //数组包含{@“Chris”,@“Melissa”}

//        

}

        

//实现 UIsearchBarDelegate 协议的方法

//获取焦点,成为第一响应者

-(BOOL)searchBarShouldBeginEditing:(UISearchBar*)searchBar

{

    self.searchBar.showsScopeBar = TRUE;

    [self.searchBar sizeToFit];

    return YES;

    

    

}

//点击键盘上的搜索按钮

-(void)searchBarSearchButtonClicked:(UISearchBar*)searchBar

    {

        self.searchBar.showsScopeBar = NO;

        [self.searchBar sizeToFit];

        [self.searchBar resignFirstResponder];

    }

    

//点击搜素栏取消按钮

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar

{

        //查询所有

        [self filterContentForSearchText:self.searchBar.text scope:-1];

        self.searchBar.showsScopeBar = NO;

        [self.searchBar sizeToFit];

        [self.searchBar resignFirstResponder];

    }

    

//当文本内容发生改变时调用

-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)searchText

    {

        [self filterContentForSearchText:self.searchBar.text scope:self.searchBar.selectedScopeButtonIndex];

        [self.tableView reloadData];

    

    }

// 当搜索范围选择变化时调用

-(void)searchBar:(UISearchBar*)searchBar

selectedScopeButtonIndexDidChange:(NSInteger)selectedScope

{

    [self filterContentForSearchText:self.searchBar.text scope:selectedScope];

    [self.tableView reloadData];

}

    

    

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

{

   

        //按照索引从小组名数组中获得组名

        NSString *groupName = [self.listGroupname objectAtIndex:section];

        //将组名作为key,从字典中取出球队数组集合

        NSArray *listTeams = [self.dictData objectForKey:groupName];

        return [listTeams count];

    }

//-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

//{

//    return 1;

//}

-(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 section = [indexPath section];

    //获取选择节中选中行的索引

    NSUInteger row =[indexPath row];

    //按照节索引从小组名数组中获得组名

    NSString *groupName = [self.listGroupname objectAtIndex:section];

    //将小组名作为key,从字典中获取球队数组集合

    NSArray *listTeams = [self.dictData objectForKey:groupName];

    

    cell.textLabel.text = [listTeams objectAtIndex:row];

    return cell;

//    NSDictionary *rowDict = [self.listTeam objectAtIndex:row];

//    cell.textLabel.text = [rowDict objectForKey:@"name"];

//    

//    NSString*imagePath = [rowDict objectForKey:@"image"];

//    imagePath = [imagePath stringByAppendingString:@".png"];

//    cell.imageView.image = [UIImage imageNamed:imagePath];

//    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return [self.listGroupname count];

}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{

    NSString *groupName = [self.listGroupname objectAtIndex:section];

    return groupName;

}

-(NSArray*)sectionIndexTitlesForTableView: (UITableView *)tableView

{

    NSMutableArray *listTitles = [[NSMutableArray alloc]initWithCapacity:[self.listGroupname count]];

//把A组改为A

    for (NSString *item in self.listGroupname) {

        NSString *title = [item substringToIndex:1];

        [listTitles addObject:title];

        

    }

    return listTitles;

}

@end

猜你喜欢

转载自blog.csdn.net/yindusu/article/details/81206613