1. 程式人生 > >IOS 新增索引

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