你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> IOS開發(52)之UITableView索引搜索之UILocalizedIndexedCollation

IOS開發(52)之UITableView索引搜索之UILocalizedIndexedCollation

編輯:IOS開發綜合

1 前言
IOS對於TableView的表格索引還停工一個工具類--UILocalizedIndexedCollation,今天我們就來學習一下這個控件。

2 代碼實例
ZYAppDelegate.m

 

[plain]
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    // Override point for customization after application launch. 
    self.viewController = [[ZYViewController alloc] initWithNibName:@"ZYViewController" bundle:nil]; 
    self.window.rootViewController = self.viewController; 
    /* 
     創建時間分區實體類,並傳給跟視圖控制器 
     */ 
    NSArray *timeZoneNames = [NSTimeZone knownTimeZoneNames]; 
    NSMutableArray *timeZones = [[NSMutableArray alloc] initWithCapacity:[timeZoneNames count]]; 
     
    for (NSString *timeZoneName in timeZoneNames) { 
         
        NSArray *nameComponents = [timeZoneName componentsSeparatedByString:@"/"]; 
        // For this example, the time zone itself isn't needed. 
        ZYTimeZoneWrapper *timeZoneWrapper = [[ZYTimeZoneWrapper alloc] initWithTimeZone:nil nameComponents:nameComponents]; 
         
        [timeZones addObject:timeZoneWrapper]; 
        [timeZoneWrapper release]; 
    } 
     
    self.viewController.timeZonesArray = timeZones; 
    [timeZones release]; 
    [self.window makeKeyAndVisible]; 
    return YES; 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[ZYViewController alloc] initWithNibName:@"ZYViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    /*
     創建時間分區實體類,並傳給跟視圖控制器
  */
 NSArray *timeZoneNames = [NSTimeZone knownTimeZoneNames];
 NSMutableArray *timeZones = [[NSMutableArray alloc] initWithCapacity:[timeZoneNames count]];
 
 for (NSString *timeZoneName in timeZoneNames) {
  
  NSArray *nameComponents = [timeZoneName componentsSeparatedByString:@"/"];
  // For this example, the time zone itself isn't needed.
  ZYTimeZoneWrapper *timeZoneWrapper = [[ZYTimeZoneWrapper alloc] initWithTimeZone:nil nameComponents:nameComponents];
  
  [timeZones addObject:timeZoneWrapper];
  [timeZoneWrapper release];
 }
 
 self.viewController.timeZonesArray = timeZones;
 [timeZones release];
    [self.window makeKeyAndVisible];
    return YES;
}
ZYTimeZoneWrapper.h

 

[plain]
#import <Foundation/Foundation.h> 
 
@interface ZYTimeZoneWrapper : NSObject{ 
    NSString *localeName; 
    NSTimeZone *timeZone; 

//本地名稱 
@property (nonatomic, copy) NSString *localeName; 
//時間分區 
@property (nonatomic, retain) NSTimeZone *timeZone; 
//初始化,格式化對象 
- (id)initWithTimeZone:(NSTimeZone *)aTimeZone nameComponents:(NSArray *)nameComponents; 
 
@end 

#import <Foundation/Foundation.h>

@interface ZYTimeZoneWrapper : NSObject{
 NSString *localeName;
 NSTimeZone *timeZone;
}
//本地名稱
@property (nonatomic, copy) NSString *localeName;
//時間分區
@property (nonatomic, retain) NSTimeZone *timeZone;
//初始化,格式化對象
- (id)initWithTimeZone:(NSTimeZone *)aTimeZone nameComponents:(NSArray *)nameComponents;

@end
ZYTimeZoneWrapper.m

 

[plain]
@synthesize localeName, timeZone; 
 
- (id)initWithTimeZone:(NSTimeZone *)aTimeZone nameComponents:(NSArray *)nameComponents { 
     
    if (self = [super init]) { 
         
        timeZone = [aTimeZone retain]; 
         
        NSString *name = nil; 
        if ([nameComponents count] == 2) { 
            name = [nameComponents objectAtIndex:1]; 
        } 
        if ([nameComponents count] == 3) { 
            name = [NSString stringWithFormat:@"%@ (%@)", [nameComponents objectAtIndex:2], [nameComponents objectAtIndex:1]]; 
        } 
         
        localeName = [[name stringByReplacingOccurrencesOfString:@"_" withString:@" "] retain]; 
    } 
    return self; 

 
 
- (void)dealloc { 
    [localeName release]; 
    [timeZone release]; 
     
    [super dealloc]; 

@synthesize localeName, timeZone;

- (id)initWithTimeZone:(NSTimeZone *)aTimeZone nameComponents:(NSArray *)nameComponents {
 
 if (self = [super init]) {
  
  timeZone = [aTimeZone retain];
  
  NSString *name = nil;
  if ([nameComponents count] == 2) {
   name = [nameComponents objectAtIndex:1];
  }
  if ([nameComponents count] == 3) {
   name = [NSString stringWithFormat:@"%@ (%@)", [nameComponents objectAtIndex:2], [nameComponents objectAtIndex:1]];
  }
  
  localeName = [[name stringByReplacingOccurrencesOfString:@"_" withString:@" "] retain];
 }
 return self;
}


- (void)dealloc {
 [localeName release];
 [timeZone release];
 
 [super dealloc];
}
ZYViewController.h

 

[plain]
#import <UIKit/UIKit.h> 
#import "ZYTimeZoneWrapper.h" 
 
@interface ZYViewController : UITableViewController 
 
@property (nonatomic, retain) NSMutableArray *timeZonesArray; 
@property (nonatomic, retain) NSMutableArray *sectionsArray; 
//UITableView索引搜索工具類 
@property (nonatomic, retain) UILocalizedIndexedCollation *collation; 
 
@end 

#import <UIKit/UIKit.h>
#import "ZYTimeZoneWrapper.h"

@interface ZYViewController : UITableViewController

@property (nonatomic, retain) NSMutableArray *timeZonesArray;
@property (nonatomic, retain) NSMutableArray *sectionsArray;
//UITableView索引搜索工具類
@property (nonatomic, retain) UILocalizedIndexedCollation *collation;

@end
ZYViewController.m

 

[plain]
@implementation ZYViewController 
 
@synthesize timeZonesArray, sectionsArray, collation; 
 
- (void)viewDidLoad 

    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    self.title = @"Time Zones"; 

 
#pragma mark - 
#pragma mark Table view data source and delegate methods 
//設置Section的數 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    // The number of sections is the same as the number of titles in the collation. 
    return [[collation sectionTitles] count]; 

 
//設置每個Section下面的cell數 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
     
    // The number of time zones in the section is the count of the array associated with the section in the sections array. 
    NSArray *timeZonesInSection = [sectionsArray objectAtIndex:section]; 
     
    return [timeZonesInSection count]; 

 
//設置每行的cell的內容 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
     
    static NSString *CellIdentifier = @"Cell"; 
     
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 
     
    // Get the time zone from the array associated with the section index in the sections array. 
    NSArray *timeZonesInSection = [sectionsArray objectAtIndex:indexPath.section]; 
     
    // Configure the cell with the time zone's name. 
    ZYTimeZoneWrapper *timeZone = [timeZonesInSection objectAtIndex:indexPath.row]; 
    cell.textLabel.text = timeZone.localeName; 
     
    return cell; 

 
/* 
 Section-related methods: Retrieve the section titles and section index titles from the collation. 
 */ 
//設置section的Header 
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
    return [[collation sectionTitles] objectAtIndex:section]; 

 
//設置索引標題 
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { 
    return [collation sectionIndexTitles]; 

 
//關聯搜索 
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index { 
    return [collation sectionForSectionIndexTitleAtIndex:index]; 

 
 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

 
#pragma mark - 
#pragma mark Set the data array and configure the section data 
 
- (void)setTimeZonesArray:(NSMutableArray *)newDataArray { 
    if (newDataArray != timeZonesArray) { 
        [timeZonesArray release]; 
        timeZonesArray = [newDataArray retain]; 
    } 
    if (timeZonesArray == nil) { 
        self.sectionsArray = nil; 
    } 
    else { 
        [self configureSections]; 
    } 

 
- (void)configureSections { 
     
    //獲得當前UILocalizedIndexedCollation對象並且引用賦給collation 
    self.collation = [UILocalizedIndexedCollation currentCollation]; 
    //獲得索引數和section標題數 
    NSInteger index, sectionTitlesCount = [[collation sectionTitles] count]; 
     
    NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount]; 
     
    //設置sections數組:元素包含timezone 
    for (index = 0; index < sectionTitlesCount; index++) { 
        NSMutableArray *array = [[NSMutableArray alloc] init]; 
        [newSectionsArray addObject:array]; 
        [array release]; 
    } 
     
    // Segregate the time zones into the appropriate arrays. 
    for (ZYTimeZoneWrapper *timeZone in timeZonesArray) { 
         
        //根據timezone的localename,獲得對應的時區的section number 
        NSInteger sectionNumber = [collation sectionForObject:timeZone collationStringSelector:@selector(localeName)]; 
         
        //獲得section的數組 
        NSMutableArray *sectionTimeZones = [newSectionsArray objectAtIndex:sectionNumber]; 
         
        //添加時區內容到section中 
        [sectionTimeZones addObject:timeZone]; 
    } 
     
    //排序 
    for (index = 0; index < sectionTitlesCount; index++) { 
         
        NSMutableArray *timeZonesArrayForSection = [newSectionsArray objectAtIndex:index]; 
         
        //獲得排序結果 
        NSArray *sortedTimeZonesArrayForSection = [collation sortedArrayFromArray:timeZonesArrayForSection collationStringSelector:@selector(localeName)]; 
         
        //替換原來數組 
        [newSectionsArray replaceObjectAtIndex:index withObject:sortedTimeZonesArrayForSection]; 
    } 
     
    self.sectionsArray = newSectionsArray; 
    [newSectionsArray release]; 

 
#pragma mark - 
#pragma mark Memory management 
 
- (void)dealloc { 
    [timeZonesArray release]; 
    [sectionsArray release]; 
    [collation release]; 
    [super dealloc]; 

@implementation ZYViewController

@synthesize timeZonesArray, sectionsArray, collation;

- (void)viewDidLoad
{
    [super viewDidLoad];
 // Do any additional setup after loading the view, typically from a nib.
    self.title = @"Time Zones";
}

#pragma mark -
#pragma mark Table view data source and delegate methods
//設置Section的數
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
 // The number of sections is the same as the number of titles in the collation.
    return [[collation sectionTitles] count];
}

//設置每個Section下面的cell數
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
 
 // The number of time zones in the section is the count of the array associated with the section in the sections array.
 NSArray *timeZonesInSection = [sectionsArray objectAtIndex:section];
 
    return [timeZonesInSection count];
}

//設置每行的cell的內容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    static NSString *CellIdentifier = @"Cell";
   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
   
 // Get the time zone from the array associated with the section index in the sections array.
 NSArray *timeZonesInSection = [sectionsArray objectAtIndex:indexPath.section];
 
 // Configure the cell with the time zone's name.
 ZYTimeZoneWrapper *timeZone = [timeZonesInSection objectAtIndex:indexPath.row];
    cell.textLabel.text = timeZone.localeName;
 
    return cell;
}

/*
 Section-related methods: Retrieve the section titles and section index titles from the collation.
 */
//設置section的Header
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [[collation sectionTitles] objectAtIndex:section];
}

//設置索引標題
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return [collation sectionIndexTitles];
}

//關聯搜索
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    return [collation sectionForSectionIndexTitleAtIndex:index];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
 [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark -
#pragma mark Set the data array and configure the section data

- (void)setTimeZonesArray:(NSMutableArray *)newDataArray {
 if (newDataArray != timeZonesArray) {
  [timeZonesArray release];
  timeZonesArray = [newDataArray retain];
 }
 if (timeZonesArray == nil) {
  self.sectionsArray = nil;
 }
 else {
  [self configureSections];
 }
}

- (void)configureSections {
 
    //獲得當前UILocalizedIndexedCollation對象並且引用賦給collation
 self.collation = [UILocalizedIndexedCollation currentCollation];
 //獲得索引數和section標題數
 NSInteger index, sectionTitlesCount = [[collation sectionTitles] count];
 
 NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount];
 
    //設置sections數組:元素包含timezone
 for (index = 0; index < sectionTitlesCount; index++) {
  NSMutableArray *array = [[NSMutableArray alloc] init];
  [newSectionsArray addObject:array];
  [array release];
 }
 
 // Segregate the time zones into the appropriate arrays.
 for (ZYTimeZoneWrapper *timeZone in timeZonesArray) {
  
        //根據timezone的localename,獲得對應的時區的section number
  NSInteger sectionNumber = [collation sectionForObject:timeZone collationStringSelector:@selector(localeName)];
  
        //獲得section的數組
  NSMutableArray *sectionTimeZones = [newSectionsArray objectAtIndex:sectionNumber];
  
        //添加時區內容到section中
  [sectionTimeZones addObject:timeZone];
 }
 
    //排序
 for (index = 0; index < sectionTitlesCount; index++) {
  
  NSMutableArray *timeZonesArrayForSection = [newSectionsArray objectAtIndex:index];
  
  //獲得排序結果
  NSArray *sortedTimeZonesArrayForSection = [collation sortedArrayFromArray:timeZonesArrayForSection collationStringSelector:@selector(localeName)];
  
  //替換原來數組
  [newSectionsArray replaceObjectAtIndex:index withObject:sortedTimeZonesArrayForSection];
 }
 
 self.sectionsArray = newSectionsArray;
 [newSectionsArray release];
}

#pragma mark -
#pragma mark Memory management

- (void)dealloc {
 [timeZonesArray release];
 [sectionsArray release];
 [collation release];
    [super dealloc];
}
運行結果:

 

\

 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved