0

iOS のアドレス帳から連絡先を取得しようとしています。

私のNSLOGではすべて問題ないようですが、すべての連絡先をテーブルビュー(ラベルなど)に配置すると、白いセルとアクセサリのみが3回表示されます(連絡先は3つしかなく、この場合はカウントがうまく機能しています)

@interface ViewController ()
@end

@implementation ViewController

@synthesize  searchBar,contactsTableView, retrievedContacts;



 - (void)viewDidLoad {

    [super viewDidLoad];

    self.title = @"All Contacts";
    self.tableData = [[NSMutableArray alloc] init];
    retrievedContacts = [[NSMutableArray alloc] init];

    self.contactsTableView.dataSource = self;
    self.contactsTableView.delegate = self;




    [self getPersons];

    [self.contactsTableView reloadData];

}


-(void)getPersons {
   // [retrievedContacts removeAllObjects];
    int i;
    ABAddressBookRef contactBook = ABAddressBookCreate();
    NSMutableArray *allData = (__bridge_transfer NSMutableArray *)(ABAddressBookCopyArrayOfAllPeople(contactBook));
    CFIndex contactNum = CFArrayGetCount((__bridge CFArrayRef)(allData));

    for (i = 0; i < contactNum; i++) {
        ABRecordRef ref = CFArrayGetValueAtIndex((__bridge CFMutableArrayRef)(allData), i);
        firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
        lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty);
        phonesNum = ABRecordCopyValue(ref, kABPersonPhoneProperty);
        [retrievedContacts addObject:(__bridge id)(firstName)];
        [retrievedContacts addObject:(__bridge id)(lastName)];
        [retrievedContacts addObject:(__bridge id)(phonesNum)];

        NSLog(@"First name %@", firstName);
        NSLog(@"Last Name %@", lastName);
        NSLog(@"Phone %@", phonesNum);

    }

    NSLog(@"Count Contacts %li", contactNum);
    //self.tableData = retrievedContacts;

}





-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [retrievedContacts count];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"Cell";

    abCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    if (!cell) {
       cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    }

    cell.firstNameLabel.text = [retrievedContacts objectAtIndex:indexPath.row];
    cell.lastNameLabel.text = [retrievedContacts objectAtIndex:indexPath.row];

    return cell;


}

そしてここで NSLOG 出力:

2013-05-28 15:50:39.260 GTCallBack[39242:c07] First name: Anton
2013-05-28 15:50:39.261 GTCallBack[39242:c07] Last Name: SAnton
2013-05-28 15:50:39.262 GTCallBack[39242:c07] Phone numbers: ABMultiValueRef 0x797e6e0 with 2 value(s)
    0: _$!<Mobile>!$_ (0x797ee30) - +972 (58) 123 4567 (0x797ee50)
    1: iPhone (0x7976cc0) - +972 (58) 123 4567 (0x797ee10)
2013-05-28 15:50:39.262 GTCallBack[39242:c07] Contact Image: Anton
2013-05-28 15:50:39.263 GTCallBack[39242:c07] First name: Anton
2013-05-28 15:50:39.263 GTCallBack[39242:c07] Last Name: Anton
2013-05-28 15:50:39.264 GTCallBack[39242:c07] Phone numbers: ABMultiValueRef 0x6d8c560 with 1 value(s)
    0: _$!<Mobile>!$_ (0x6d8c940) - (058) 123 4567 (0x6d8c960)
2013-05-28 15:50:39.268 GTCallBack[39242:c07] Contact Image: Anton
2013-05-28 15:50:39.269 GTCallBack[39242:c07] First name: Shalom
2013-05-28 15:50:39.270 GTCallBack[39242:c07] Last Name: Shalom
2013-05-28 15:50:39.270 GTCallBack[39242:c07] Phone numbers: ABMultiValueRef 0x7c689d0 with 1 value(s)
    0: _$!<Mobile>!$_ (0x7c604c0) - (058) 123 4567 (0x7c6bff0)
2013-05-28 15:50:39.271 GTCallBack[39242:c07] Contact Image: Shalom
2013-05-28 15:50:39.301 GTCallBack[39242:c07] Count Contacts 3

連絡先データでテーブルを埋める別の方法があるでしょうか?

ありがとう

4

3 に答える 3

0

昨日、課題を書き終えました。今、すべての作業。

これがコードです。

 #import "ViewController.h"
#import "ContactDetailsViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize  searchBar, contactsTableView;


- (void)viewDidLoad {
    [super viewDidLoad];

    contacts = [[Contacts alloc] init];
    [contacts getContacts];

    self.title = @"All Contacts";

    self.contactsTableView.dataSource = self;
    self.contactsTableView.delegate = self;    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [contacts.firstNames count];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"Cell";

    abCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    if (!cell) {
       cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    }



    cell.firstNameLabel.text = [contacts.firstNames objectAtIndex:indexPath.row]; 
    cell.lastNameLabel.text = [contacts.lastNames objectAtIndex:indexPath.row];

    /*for (NSString *value in [retrievedContacts objectAtIndex:indexPath.row]) {
        NSMutableArray *tmp = [[NSMutableArray alloc] init];

        [tmp addObject:[[retrievedContacts objectAtIndex:indexPath.row] valueForKey:value]];
        NSLog(@"Contact: %@", tmp);
    }*/

    /*NSLog(@"Name: %@", [[retrievedContacts objectAtIndex:indexPath.row] valueForKey:@"First Name"]);
    NSLog(@"Familie: %@", [[retrievedContacts objectAtIndex:indexPath.row] valueForKey:@"Last Name"]);


    NSLog(@"PHONE TYPE: %@ • PHONE NUMBER: %@", [[[retrievedContacts objectAtIndex:indexPath.row] objectForKey:@"Phones"] valueForKey: @"Phone Label"], [[[retrievedContacts objectAtIndex:indexPath.row] objectForKey:@"Phones"] valueForKey: @"Phone Number"]);*/

    return cell;


}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [self.contactsTableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {


    if ([[segue identifier] isEqualToString:@"NextView"]) {

        ContactDetailsViewController *nextController = [segue destinationViewController];
        NSIndexPath *indexPath = [[[self contactsTableView] indexPathsForSelectedRows] objectAtIndex:0];
        [nextController setPhoneTypes:[contacts.phoneTypes objectAtIndex:[indexPath row]]];
        [nextController setPhoneNumbers:[contacts.phoneNumbers objectAtIndex:[indexPath row]]];

    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end




    #import "Contacts.h"

@implementation Contacts
@synthesize firstNames, lastNames, phoneTypes, phoneNumbers, photos, retrievedContacts;

-(void)getContacts {

    ABAddressBookRef addressBook = ABAddressBookCreate();

    __block BOOL accessGranted = NO;
    if (ABAddressBookRequestAccessWithCompletion != NULL) { // we're on iOS 6
        dispatch_semaphore_t sema = dispatch_semaphore_create(0);
        ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
            accessGranted = granted;
            dispatch_semaphore_signal(sema);
        });
        dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
        dispatch_release(sema);
    }
    else { // we're on iOS 5 or older
        accessGranted = YES;
    }

    if (accessGranted) {

        int i;

        firstNames = [[NSMutableArray alloc] init];
        lastNames = [[NSMutableArray alloc] init];
        phoneTypes = [[NSMutableArray alloc] init];
        phoneNumbers = [[NSMutableArray alloc] init];
        photos = [[NSMutableArray alloc] init];

        ABAddressBookRef contactBook = ABAddressBookCreate();
        NSMutableArray *allData = (__bridge_transfer NSMutableArray *)(ABAddressBookCopyArrayOfAllPeople(contactBook));
        contactNum = CFArrayGetCount((__bridge CFArrayRef)(allData));

        retrievedContacts  = [[NSMutableArray alloc] init];

        for (i = 0; i < contactNum; i++) {

            ABRecordRef ref = CFArrayGetValueAtIndex((__bridge CFMutableArrayRef)(allData), i);
            firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
            lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty);
            phonesNum = ABRecordCopyValue(ref, kABPersonPhoneProperty);
            ContactImg = ABRecordCopyValue(ref, kABPersonImageFormatThumbnail);

            ABMutableMultiValueRef phoneNum = ABRecordCopyValue(ref, kABPersonPhoneProperty);
            CFIndex phoneNumberCount = ABMultiValueGetCount( phoneNum );

            if (!firstName) {

                [firstNames addObject:@"NO NAME"];
                //NSLog(@"FN %@ • LN %@ • CI %@", firstName, lastName, ContactImg);
            } else if (!lastName) {

                [lastNames addObject:@"NO LAST NAME"];

            } else if (!ContactImg) {

                [photos addObject:@"NOIMG.png"];

            } else {

                /*NSMutableArray *person = [[NSMutableArray alloc] init];
                 [person addObject:(__bridge id)(firstName)];
                 [person addObject:(__bridge id)(lastName)];
                 //[person addObject:(__bridge id)(phonesNum)];
                 [person addObject:(__bridge id)(ContactImg)];
                 [retrievedContacts addObject:person];*/

                [firstNames addObject:(__bridge id)(firstName)];
                [lastNames addObject:(__bridge id)(lastName)];
                [photos addObject:(__bridge id)(ContactImg)];

                NSMutableArray *tmpType = [[NSMutableArray alloc] init];
                NSMutableArray *tmpNumbr = [[NSMutableArray alloc] init];

                for (int k = 0; k < phoneNumberCount; k++ )
                {
                    CFStringRef phoneNumberLabel = ABMultiValueCopyLabelAtIndex( phoneNum, k );
                    CFStringRef phoneNumberValue = ABMultiValueCopyValueAtIndex( phoneNum, k );
                    CFStringRef phoneNumberLocalizedLabel = ABAddressBookCopyLocalizedLabel( phoneNumberLabel );    // converts "_$!<Work>!$_" to "work" and "_$!<Mobile>!$_" to "mobile"


                    NSLog(@"-----PHONE ENTRY -> %@ : %@\nPERSON NUM. %i • PHONE NUM. %i", phoneNumberLocalizedLabel, phoneNumberValue, i, k );

                    [tmpType addObject:(__bridge id)(phoneNumberLocalizedLabel)];
                    [tmpNumbr addObject:(__bridge id)(phoneNumberValue)];

                    CFRelease(phoneNumberLocalizedLabel);
                    CFRelease(phoneNumberLabel);
                    CFRelease(phoneNumberValue);
                }

                [phoneTypes addObject:tmpType];
                [phoneNumbers addObject:tmpNumbr];

#ifdef DEBUG
                //NSLog(@"First name: %@", firstName);
                //NSLog(@"Last Name: %@", lastName);
                //NSLog(@"Phone numbers: %@", phonesNum);
                //NSLog(@"Contact Image: %@", ContactImg);
                //NSLog(@"Some Text %@", retrievedContacts); //Here everething is OK :)
#endif
            }
        }
    }
}



@end

それが誰かの時間を節約することを願っています。

ありがとうございます

于 2013-05-29T10:26:24.060 に答える