1.要 导入系统的 2个包
//
// TheAddressBook.m
// Message
//
// Created by bml on 14-6-3.
// Copyright (c) 2014年 bml. All rights reserved.
//
#import "TheAddressBook.h"
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
#import "MyCell.h"
@interface TheAddressBook ()
{
int selectIndex;
}
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property (nonatomic,strong)NSMutableArray *phoneName;
@property (nonatomic,strong)NSMutableArray *phoneNum;
@property (nonatomic,strong)NSMutableArray *array;
@end
@implementation TheAddressBook
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
selectIndex =-1;
// NSString *personName22 = nil;
// NSString *name =@"0.0";
// name=[personName stringByAppendingString:personName];
self.phoneName = [NSMutableArray new];
self.phoneNum = [NSMutableArray new];
ABAddressBookRef addressBook = nil;
//判断版本
if ([UIDevice currentDevice].systemVersion.floatValue>=6.0) {
addressBook=ABAddressBookCreateWithOptions(NULL, NULL);
dispatch_semaphore_t sema=dispatch_semaphore_create(0);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error)
{
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
//dispatch_release(sema);
}
else
{
addressBook=ABAddressBookCreateWithOptions(NULL, NULL);
}
CFArrayRef results = ABAddressBookCopyArrayOfAllPeople(addressBook);
for(int i = 0; i < CFArrayGetCount(results); i++)
{
ABRecordRef person = CFArrayGetValueAtIndex(results, i);
//读取firstname
//读取电话多值
ABMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty);
for (int k = 0; k<ABMultiValueGetCount(phone); k++)
{
//获取电话Label
// NSString * personPhoneLabel = (__bridge NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(phone, k));
//获取該Label下的电话值
NSString * personPhone = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phone, k);
//获取姓
NSString *personName = (__bridge NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
//获取名
NSString *lastname = (__bridge NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);
//将姓名组合起来
// name=[personName stringByAppendingString:lastname];
if (personName !=nil)
{
[self.phoneName addObject:personName];
NSLog(@" phoneName is :%@",self.phoneName);
}
if (lastname !=nil) {
[self.phoneName addObject:lastname];
}
// NSLog(@"name is value --->%@",name);
// NSLog(@"%@",personPhone);
// NSLog(@"%@",personName);
// NSLog(@"%@",lastname);
// [self.phoneName addObject:personName];
// [self.phoneNum addObject:personPhone];
// [self.phoneName addObject:personName];
NSLog(@" phoneName is :%@",self.phoneName);
}
// [self.array addObject:self.phoneName];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"self.phoneName.count is :%zi",self.phoneName.count);
return self.phoneName.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentfier = @"cell";
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentfier forIndexPath:indexPath];
cell.topTile.text = [self.phoneName objectAtIndex:indexPath.row];
// cell.subTutle.text = [self.phoneName objectAtIndex:indexPath.row];
cell.btn1.selected =NO;
[cell.btn1 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
// if (cell.btn1.selected ==) {
// <#statements#>
// }
[cell.btn1 setTag:indexPath.row];
return cell;
}
-(void)onClick:(UIButton *)sender
{
int row = sender.tag;
// MyCell *cell = (MyCell *)[self.myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0]];
// if (sender.selected) {
// sender.selected = NO;
// NSLog(@"第二次相应事件");
//
// [cell.btn2 setHidden:NO];
//
// }else {
// sender.selected = YES;
// [cell.btn2 setHidden:YES];
// NSLog(@"第一次相应事件");
// }
NSLog(@"cell is btn ---->%zi",row);
if (selectIndex != row) {
NSLog(@"selectIndex is value :%zi",selectIndex);
if (selectIndex != -1) {
MyCell *cell = (MyCell *)[self.myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:selectIndex inSection:0]];
[cell.btn2 setHidden:NO];
// [cell.btn setImage:[UIImage imageNamed:@"box_chenck"] forState:UIControlStateNormal];
}
selectIndex = row;
MyCell *cell = (MyCell *)[self.myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0]];
[cell.btn2 setHidden:YES];
// [cell.btn setImage:[UIImage imageNamed:@"box_chencked"] forState:UIControlStateNormal];
// ToPublishCircleBean *bean3 = [self.cellData objectAtIndex:row];
// self.string= bean3.title;
}
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end