读取电话薄内容在TableView中显示

首先添加AddressBook.framework类库.再#inport到头文件里.

具体代码如下:(分组首字母没有进行排序)

#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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值