在电话联系人订单的UItableview中显示联系人

时间:2011-05-03 05:28:43

标签: iphone

我可以从电话簿导入联系人并在表格视图中显示它们。但我想要做的是按照电话簿顺序显示联系人的顺序..... / p>

任何人都可以帮我解决这个问题,我的代码如下

self.navigationController.navigationBar.tintColor = [UIColor grayColor];
    self.title = @"iPhone Contacts";
    [super viewDidLoad];
    wantedname= [[NSMutableArray alloc] init];
    wantednumber= [[NSMutableArray alloc] init];
    ABAddressBookRef addressBook = ABAddressBookCreate();
    NSArray *thePeople = (NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);

    NSString *name;
    for (id person in thePeople)
    {
        name = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
        NSLog(@"!!!!!! name ---> %@",name);
        ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonPhoneProperty);
        int count1=ABMultiValueGetCount(multi);
        NSLog(@"%d",count1);
        if ([name length]>0 && count1!=0) 
        {
                    NSString *beforenumber = (NSString *)ABMultiValueCopyValueAtIndex(multi, 0);
            NSLog(@" contacts:%@",beforenumber );
            NSString* removed1=[beforenumber stringByReplacingOccurrencesOfString:@"-"withString:@""];
            NSString* removed2=[removed1 stringByReplacingOccurrencesOfString:@")"withString:@""];
            NSString* removed3=[removed2 stringByReplacingOccurrencesOfString:@" "withString:@""];
            NSString* removed4=[removed3 stringByReplacingOccurrencesOfString:@"("withString:@""];
            NSString* removed5=[removed4 stringByReplacingOccurrencesOfString:@"+"withString:@""];
            [wantedname addObject:name];
            [wantednumber addObject:removed5];
           // CFRelease(beforenumber);
            [beforenumber release];
            //CFRelease(name);

        }
        //CFRelease(name);
        [name release];
        CFRelease(multi);
    }

    CFRelease(addressBook);
    CFRelease(thePeople);

    contactstable.delegate = self;
    contactstable.dataSource = self;

2 个答案:

答案 0 :(得分:11)

而不是:

NSArray *thePeople = (NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);

您可以尝试:

ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
NSArray *thePeople = (NSArray*)ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByLastName);

答案 1 :(得分:1)

这就是我做的。希望能帮助到你。 :)

ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);

CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFMutableArrayRef allPeopleMutable = CFArrayCreateMutableCopy(kCFAllocatorDefault, CFArrayGetCount(allPeople), allPeople);
CFArraySortValues(allPeopleMutable, CFRangeMake(0, CFArrayGetCount(allPeopleMutable)), (CFComparatorFunction)ABPersonComparePeopleByName, kABPersonSortByFirstName);