#import "ViewController.h"
#import
@interface ViewController ()
@property (nonatomic, retain) NSMutableDictionary *personNamesByIndex;
@property (nonatomic, retain) NSMutableDictionary *personCompaniesByIndex;
@property (nonatomic, retain) NSArray *personIndexes;
@end
@implementation ViewController
-(void)dealloc
{
[_personNamesByIndex release];
[_personCompaniesByIndex release];
[_personIndexes release];
[super dealloc];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.personNamesByIndex = [NSMutableDictionary dictionary];
self.personCompaniesByIndex = [NSMutableDictionary dictionary];
NSMutableSet *indexes = [NSMutableSet set];
// 创建地址簿对象
ABAddressBookRef addressBook = ABAddressBookCreate();
// 从地址簿对象里获得联系人数组
CFArrayRef contacts = ABAddressBookCopyArrayOfAllPeople(addressBook);
// 对联系人数组进行轮询
for (int i=0; i<<span style="color: #3d1d81">CFArrayGetCount(contacts); i++)
{
// 获得数组中的当前联系人
ABRecordRef person = CFArrayGetValueAtIndex(contacts, i);
CFStringRef personName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
CFStringRef personCompany = ABRecordCopyValue(person, kABPersonOrganizationProperty);
NSLog(@"%@, %@", personName, personCompany);
// 首字母
NSString *personIndex = [(NSString *)personName substringToIndex:1];
// 为了演示起见添加6次
for (int j=0; j<<span style="color: #272ad8">6; j++)
{
[indexes addObject:personIndex];
// 判断这个首字母是否有对应的联系人数组
NSMutableArray *names = [self.personNamesByIndex objectForKey:personIndex];
if (!names)
{
names = [NSMutableArray array];
[self.personNamesByIndex setObject:names forKey:personIndex];
}
[names addObject:(NSString *)personName];
// 判断这个首字母是否有对应的公司名数组
NSMutableArray *companies = [self.personCompaniesByIndex objectForKey:personIndex];
if (!companies)
{
companies = [NSMutableArray array];
[self.personCompaniesByIndex setObject:companies forKey:personIndex];
}
[companies addObject:(NSString *)personCompany];
}// end of for 6次
if (personName)
{
CFRelease(personName);
}
if (personCompany)
{
CFRelease(personCompany);
}
}
CFRelease(contacts);
CFRelease(addressBook);
// 将索引set转为数组
self.personIndexes = [indexes allObjects];
NSLog(@"%@", self.personIndexes);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.personIndexes count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSString *personIndex = [self.personIndexes objectAtIndex:section];
return [[self.personNamesByIndex objectForKey:personIndex] count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 标示符
static NSString *identifier = @"PersonCell";
// 试图重用单元格
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier] autorelease];
}
// 取得当前section对应的索引名
NSString *personIndex = [self.personIndexes objectAtIndex:indexPath.section];
// 根据索引名取得姓名字符串
NSString *personName = [[self.personNamesByIndex objectForKey:personIndex] objectAtIndex:indexPath.row];
// 根据索引名取得公司名字符串
NSString *personCompany = [[self.personCompaniesByIndex objectForKey:personIndex] objectAtIndex:indexPath.row];
// 为单元格设置属性
cell.textLabel.text = personName;
cell.detailTextLabel.text = personCompany;
return cell;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
// 取得当前section对应的索引名
return [self.personIndexes objectAtIndex:section];
}
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return self.personIndexes;
}
// 当section数和索引数不一致时须改写此方法
// 如果一致可以不写
-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
return index;
}
@end