关于从iPhone地址簿创建UITableView的差事的问题

时间:2011-03-13 17:11:24

标签: uitableview memory-management ios4 addressbook

我对iOS的AddressBook Framework有疑问。情况如下:

我正在尝试从手机应用程序重新创建联系人视图,但我想在同一视图中显示联系人的电话号码。因此,如果联系人有多个号码,他的名字将多次出现在TableView中,每次都有不同的号码。

我试图通过在视图加载时提取我需要的所有信息来实现这一点,之后,使用包含联系人信息的NSDictionaries组成的NSArray中的相应值填充TableView。

除了一件事之外,它的效果很好......联系人的电话号码和标签被正确读取并存储在字典中,但是当我稍后阅读它们时,它们似乎已经消失了。

这是我生成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];

1 个答案:

答案 0 :(得分:0)

最后可以自己解决这个问题。显然我正在为他们无法处理的词典添加一些零图像。