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

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

@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];
}

运行结果:


3 结语

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

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

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值