我有一個關於iOS地址簿框架的問題。情況如下:關於從iPhone地址簿創建UITableView差事問題
我試圖從手機應用程序重新創建聯繫人視圖,但我想在同一視圖中顯示聯繫人的電話號碼。所以如果一個聯繫人有多個號碼,他的名字將會多次出現在TableView中,每次使用不同的號碼。
我想通過提取視圖加載時需要的所有信息來完成該操作,之後,使用包含聯繫人信息的NSDictionaries組成的NSArray中的適當值填充TableView。
這個工程除了一件事偉大的...聯繫人的PHONENUMBERS和標籤被正確讀取並存儲在字典中,但是當我後來讀出來,他們似乎已經消失了。
這裏是我的代碼生成的NSDictionaries,我敢打賭這是某種內存管理錯誤或完全愚蠢的東西。我希望任何人都可以幫助我,非常感謝!
persons = [[NSMutableArray alloc] init];
ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
NSArray *people = (NSArray *)ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByLastName);
for (id record in people)
{
ABMultiValueRef numbers = ABRecordCopyValue((ABRecordRef)record, kABPersonPhoneProperty);
for (CFIndex i = 0; i < ABMultiValueGetCount(numbers); ++i) {
CFStringRef label = ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(numbers, i));
CFStringRef number = ABMultiValueCopyValueAtIndex(numbers, i);
CFStringRef firstNameRef = ABRecordCopyValue((ABRecordRef)record, kABPersonFirstNameProperty);
CFStringRef lastNameRef = ABRecordCopyValue((ABRecordRef)record, kABPersonLastNameProperty);
CFDataRef imageDataRef = ABPersonCopyImageDataWithFormat((ABRecordRef)record, kABPersonImageFormatThumbnail);
NSString *firstName = [NSString stringWithFormat:@"%@", firstNameRef];
NSString *lastName = [NSString stringWithFormat:@"%@", lastNameRef];
NSString *pLabel = [[NSString alloc] initWithFormat:@"%@", label];
NSString *pNumber = [NSString stringWithFormat:@"%@", number];
UIImage *image = [UIImage imageWithData:(NSData*)imageDataRef];
NSDictionary *personDict = [NSDictionary dictionaryWithObjectsAndKeys:firstName, @"firstName", lastName, @"lastName", image, @"image", pNumber, @"phoneNumber", pLabel, @"label", nil];
NSLog(@"In %@ - %@", pLabel, pNumber);
[persons addObject:personDict];
CFRelease(firstNameRef);
CFRelease(lastNameRef);
//CFRelease(imageDataRef);
CFRelease(label);
CFRelease(number);
}
}
CFRelease(addressBook);
CFRelease(source);
[people release];
當我嘗試在設備上進行調試時,我自己有一種解決方案。看起來這種行爲只在模擬器中很明顯。在我的iPhone上它主要工作,但不適用於所有聯繫人。 – BenniBrightside 2011-03-13 17:16:06