1

AddressBook からすべての連絡先を取得し、次の詳細を Mutable 配列に格納しようとしています。

プロパティは次のとおりです。

@property (nonatomic, assign) ABAddressBookRef addressBook;
@property (nonatomic, strong) NSMutableArray *contactList;
@property (nonatomic, strong) IBOutlet UITableView *contactsTableView;

すべての連絡先を取得する方法

- (void)getAllContacts {
    //line moved inside For loop as per Amar's answer. 
    //NSMutableDictionary *personModel = [[NSMutableDictionary alloc]initWithCapacity:0];
    self.addressBook = ABAddressBookCreateWithOptions(NULL, NULL); //iOS 6 and above
    CFArrayRef cList = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(self.addressBook, NULL, kABPersonSortByFirstName);
    CFIndex nPeople = ABAddressBookGetPersonCount(self.addressBook);
    for (int i=0; i<nPeople; i++) {
        //Moving this line here per Amar's answer below. The code works perfectly now.
        NSMutableDictionary *personModel = [[NSMutableDictionary alloc]initWithCapacity:0];
        ABRecordRef personRef = CFArrayGetValueAtIndex(cList, i); // Person will have name, phone number, address, email id and contact image

        //Get the name
        NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(personRef, kABPersonFirstNameProperty));
        NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(personRef, kABPersonLastNameProperty));
        NSString *name = nil;
        if(firstName!=nil && lastName!=nil) { //both names are available
            name = [NSString stringWithFormat:@"%@ %@",firstName,lastName];
        } else if(firstName!=nil && lastName==nil) { //last name not available
            name = [NSString stringWithFormat:@"%@",firstName];
        } else if(firstName==nil && lastName!=nil) { //first name not available
            name = [NSString stringWithFormat:@"%@",lastName];
        } else {
            name = @"Unnamed Contact"; //both names not available
        }
        //Get the phone numbers
        ABMultiValueRef phoneRef = ABRecordCopyValue(personRef, kABPersonPhoneProperty);
        NSMutableArray *phoneNumbers = [NSMutableArray new];
        CFIndex ctr = ABMultiValueGetCount(phoneRef);
        if(ctr!=0) {
            NSString *phoneNumber = nil;
            for (CFIndex i=0; i<ctr; i++) {
                phoneNumber = (__bridge NSString *) ABMultiValueCopyValueAtIndex(phoneRef, i);
                [phoneNumbers addObject:phoneNumber];
            }
        } else {
            [phoneNumbers addObject:@"Phone not available"];
        }

        //Get the contact address
        ABMultiValueRef addrRef = ABRecordCopyValue(personRef, kABPersonAddressProperty);
        NSMutableArray *addresses = [NSMutableArray new];
        ctr = ABMultiValueGetCount(addrRef);
        if(ABMultiValueGetCount(addrRef)!=0) {
            for(CFIndex i=0; i<ABMultiValueGetCount(addrRef); i++) {
                CFDictionaryRef addr = ABMultiValueCopyValueAtIndex(addrRef, i);
                NSString *street = (__bridge NSString *)CFDictionaryGetValue(addr, kABPersonAddressStreetKey);
                NSString *city = (__bridge NSString *)CFDictionaryGetValue(addr, kABPersonAddressCityKey);
                NSString *state = (__bridge NSString *)CFDictionaryGetValue(addr, kABPersonAddressStateKey);
                NSString *zip = (__bridge NSString *)CFDictionaryGetValue(addr, kABPersonAddressZIPKey);
                NSString *address = [NSString stringWithFormat:@"%@, %@, %@ %@",street,city,state,zip];
                [addresses addObject:address];
            }
        } else {
            [addresses addObject:@"Address not available"];
        }

        //Get the email address
        ABMultiValueRef emailRef = ABRecordCopyValue(personRef, kABPersonEmailProperty);
        NSMutableArray *emailAddresses = [NSMutableArray new];
        ctr = ABMultiValueGetCount(emailRef);
        if(ctr!=0) {
            for(CFIndex i=0; i<ctr; i++) {
                NSString *eId = (__bridge NSString*)ABMultiValueCopyValueAtIndex(emailRef, i);
                [emailAddresses addObject:eId];
            }
        } else {
            [emailAddresses addObject:@"EmailID not available"];
        }

        //Get the contact image
        UIImage *image = nil;
        if(ABPersonHasImageData(personRef)) image = (__bridge UIImage *)(ABPersonCopyImageDataWithFormat(personRef, kABPersonImageFormatThumbnail));

        //Append the values to a dictionary
        [personModel setValue:name forKey:@"cName"];
        [personModel setValue:phoneNumbers forKey:@"cPhone"];
        [personModel setValue:addresses forKey:@"cAddresses"];
        [personModel setValue:emailAddresses forKey:@"cEmailID"];
        [personModel setValue:image forKey:@"cImage"];

        [self.contactList addObject: personModel];
    }
}

tableView のデータソース メソッドで cellForRowAtIndexPath

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ContactsCell forIndexPath:indexPath];

    cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:16];
    cell.textLabel.textColor = [UIColor blackColor];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:14.0];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    NSDictionary *model = [self.contactList objectAtIndex:indexPath.row];
    NSLog(@"Name:%@",[model valueForKey:@"cName"]);
    cell.textLabel.text =[model valueForKey:@"cName"];
    return cell;
}

私のアドレス帳には 4 つの連絡先があります。ただし、私のtableViewは常に最後の連絡先の名前を返します(姓名がないため、「名前のない連絡先」です)。

Unnamed Contact
Unnamed Contact
Unnamed Contact
Unnamed Contact

理由はありますか?

4

2 に答える 2

2

それは、この行だからです。

NSMutableDictionary *personModel = [[NSMutableDictionary alloc]initWithCapacity:0];

- ループの外にありforます。連絡先リストを反復して同じ辞書を変更する前に、辞書を一度だけ作成しています。したがって、常に最後の連絡先情報が保存されます。

代わりに、上記のコード行を for ループ内に移動すると、リスト内の各連絡先を格納するための新しい辞書が作成されます。

それが役立つことを願っています!

于 2013-09-30T05:56:44.843 に答える