0

アドレス帳を使用するアプリがあります。を使用して、アドレス帳から並べ替えられた名前のリストを表示しようとしています

sortedArray = [arr_contactList sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

ユーザーが連絡先の 1 つを選択すると、その電話番号が表示されます。

iPhone のアドレス帳の電話番号を並べ替えることができます。

以下を使用して電話番号を並べ替えます。

ABRecordRef source = ABAddressBookCopyDefaultSource(ab);
NSArray *thePeople = (NSArray*)ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(ab, source, kABPersonSortByFirstName);

NSString *name;
for (id person in thePeople)
{
    name = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);

    ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);

    for(CFIndex j = 0; j < ABMultiValueGetCount(phones); j++)
    {  
        NSString* num = (NSString*)ABMultiValueCopyValueAtIndex(phones, j);

        CFStringRef locLabel1 = ABMultiValueCopyLabelAtIndex(phones, j);

        NSString *phoneLabel1 =(NSString*) ABAddressBookCopyLocalizedLabel(locLabel1);

        [tempPhoneArray addObject:num];        
    }       
}

しかし、私の実際の問題は、私の名前の配列には、リストの一番上に特殊文字で始まる連絡先があり、電話番号を選択すると、並べ替えられた連絡先リストはアルファベット A で始まるため、間違った電話番号を取得していることです。

名前の並べ替えと数字の並べ替えの両方の並べ替えを一致させるにはどうすればよいですか?

4

1 に答える 1

1

この例では、文字ごとに 1 つずつ、合計 27 の配列を作成しますが、この概念は特殊文字と大文字/小文字のチェックに適用できます。お役に立てれば。

const int capacity = 27;    
NSMutableArray *subArrays = [NSMutableArray array];

//Prepopulate the subArray with 26 empty arrays
for(int i = 0; i < capacity; i++)
    [subArrays addObject:[NSMutableArray array]];
char currFirstLetter = 'a';

for(int i = 0; i < sortedArray.count; i++)
{
    NSString *currString = [[sortedArray objectAtIndex:i] lowercaseString];

    NSLog(@"%@", currString);
    NSLog(@"%c", [currString characterAtIndex:0]);
    NSLog(@"%c", currFirstLetter);

    if([currString characterAtIndex:0] == currFirstLetter)
    {
        //65 is the position of 'a' in the ascii table, so when we subtract 97, it correlates to 0 in our array.
        [[subArrays objectAtIndex:currFirstLetter-97] addObject:[sortedArray objectAtIndex:i]];
    }
    else if([currString characterAtIndex:0] < 65 || ([currString characterAtIndex:0] > 90 && [currString characterAtIndex:0] < 97) || [currString characterAtIndex:0] > 122)
    {
        //If it's a symbol (65-90 are uppercase, 97-122 are lowercase)
        [[subArrays objectAtIndex:26] addObject:[sortedArray objectAtIndex:i]];
        //Increment the letter we're looking for, but decrement the count to try it again with the next letter
    }
    else
    {
        currFirstLetter++;
        i--;
    }
}
于 2012-05-30T02:16:29.567 に答える