IOS开发(52)之UITableView索引搜索之UILocalizedIndexedCollation

来源:互联网 发布:帆盛点胶机编程 编辑:程序博客网 时间:2024/05/01 03:48

1 前言

IOS对于TableView的表格索引还停工一个工具类--UILocalizedIndexedCollation,今天我们就来学习一下这个控件。

2 代码实例

ZYAppDelegate.m

- (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

#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

@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

#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

@implementation ZYViewController@synthesizetimeZonesArray, 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对象并且引用赋给collationself.collation = [UILocalizedIndexedCollation currentCollation];//获得索引数和section标题数NSInteger index, sectionTitlesCount = [[collation sectionTitles] count];NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount];    //设置sections数组:元素包含timezonefor (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 numberNSInteger 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];}

运行结果:


3 结语

以上就是所有内容,希望对大家有所帮助。

Demo下载地址:http://download.csdn.net/detail/u010013695/5347923

原创粉丝点击