1

呃,几天以来我一直在思考一种方法。我已经检索了所有联系人姓名并使用字典放置在一个数组中。

我拥有的是一个模型类,其中包含一个姓名列表,现在我想在联系人列表中搜索姓名的位置,这取决于我可以检索所需的联系人图像。

最初用谷歌搜索,发现一个未回答的问题与我的要求不太相似,同样可以在这里浏览

我尝试了几种方法,以下是我实现的一种方法:

编辑

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self loadReminders];

    ReminderClass *reminderToDisplay = [self.remindersArray objectAtIndex:indexPath.row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];

    // Now create the cell to display the reminder data
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellIdentifier] autorelease];
        cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
        cell.textLabel.numberOfLines = 0;
        cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0];
        cell.textLabel.adjustsFontSizeToFitWidth = YES;
    }

    tableView.backgroundColor = [UIColor clearColor];

    NSDateFormatter *dateFormat = [[[NSDateFormatter alloc]init]autorelease];
    [dateFormat setDateFormat:kDateFormat];
    NSDate *reminderDate = [dateFormat dateFromString:reminderToDisplay.Date]; 
    [dateFormat setDateFormat:kMinDateFormat]; 
    NSString *dateString = [dateFormat stringFromDate:reminderDate];

    NSString *valueString = [NSString stringWithFormat:@"%@'s %@",reminderToDisplay.Name,reminderToDisplay.Event];
    NSString *onString = [NSString stringWithFormat:@" on %@",dateString];
    NSString *reminderDetailsString = [valueString stringByAppendingString:onString];

    //Get the contact image based on name index from contact list
    ABAddressBookRef addressBook = ABAddressBookCreate( );
    CFStringRef reminderName = (CFStringRef)reminderToDisplay.Name;
    CFArrayRef allPeople = ABAddressBookCopyPeopleWithName(addressBook, reminderName);
    self.contactsList =[[[NSMutableArray alloc]init]autorelease];

    CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
    for ( int i = 0; i < nPeople; i++ ) 
    { 
    ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i); 
    NSString *contactFirstNamePart = (NSString *)ABRecordCopyValue(ref,kABPersonFirstNameProperty);
    NSString *contactFirstName = [[[NSString alloc] initWithString:contactFirstNamePart]autorelease];
    NSString *contactLastNamePart = (NSString *)ABRecordCopyValue(ref, kABPersonLastNameProperty);

    if (contactLastNamePart == nil)
    {
        self.contactName = contactFirstName;
    }

    else
    {
        NSString *contactLastName = [[[NSString alloc] initWithString:contactLastNamePart]autorelease];
        NSString *contactLastNameString = [NSString stringWithFormat:@" %@",contactLastName]; 
        self.contactName = [contactFirstName stringByAppendingString:contactLastNameString]; 
        CFRelease(contactLastNamePart);
    }

    NSDictionary *contactsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:self.contactName, kContactName, [NSNumber numberWithInt:i], kContactIndex, nil];
    [self.contactsList addObject:contactsDictionary];
    CFRelease(contactFirstNamePart);
}

NSDictionary *contactsDictionary = [self.contactsList objectAtIndex:indexPath.row]; 
self.contactName = [contactsDictionary objectForKey:kContactName]; 
int addressIndex = [[contactsDictionary objectForKey:kContactIndex]integerValue];

ABRecordRef recordReference = CFArrayGetValueAtIndex(allPeople, addressIndex);
if (ABPersonHasImageData(recordReference))
{
    NSData *imageData = (NSData *)ABPersonCopyImageData(recordReference);
    self.reminderImage = [UIImage imageWithData:imageData];
    CFRelease(imageData);
}

CFRelease(allPeople);
CFRelease(addressBook);

    UIImage *notificationImage = reminderImage;

    if (notificationImage != nil) 
    {
        UIImageView *imageView=[[[UIImageView alloc] initWithFrame:CGRectMake(240, 3, 70, 63)]autorelease];
        imageView.backgroundColor=[UIColor clearColor];
        [imageView setImage:notificationImage];
        cell.accessoryView = imageView;
    }
    else
    {
        UIImageView *imageView=[[[UIImageView alloc] initWithFrame:CGRectMake(240, 3, 70, 63)]autorelease];
        imageView.backgroundColor=[UIColor clearColor];
        UIImage *defaultImage = [UIImage imageNamed:kDefaultImage];
        [imageView setImage:defaultImage];
        cell.accessoryView = imageView;
    }
    cell.textLabel.text = reminderDetailsString;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

错误访问错误屏幕截图

在此处输入图像描述

但我无法完成所需的任务。任何人都可以指导我。

提前谢谢大家:)

4

3 回答 3

1

我正在分享我在最近的一个应用程序中使用的示例代码片段。我已经修改以适应您的要求,还请注意,我已经在记事本中编辑了它,可能有一些拼写错误。(目前我没有 mac 来测试它..:P)基本思想是在 viewDidLoad 方法中填充数据源并使用该数据源来更新 tableView。希望这将成为解决您问题的输入。

viewDidLoad

contactsToBeAdded=[[NSMutableArray alloc] init];
ABAddressBookRef addressbook = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressbook);
CFIndex numPeople = ABAddressBookGetPersonCount(addressbook);
bool hasPhoneNumber = false;
for (int i=0; i < numPeople; i++) {
    hasPhoneNumber = false;
    ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);
    ABMutableMultiValueRef phonelist = ABRecordCopyValue(person, kABPersonPhoneProperty);
    CFIndex numPhones = ABMultiValueGetCount(phonelist);


    if(numPhones > 0){
        hasPhoneNumber = true;

    }
    if(hasPhoneNumber){
        NSString *firstName=(NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
        NSString *lastName=(NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);

        CFTypeRef ABphone = ABMultiValueCopyValueAtIndex(phonelist, 0);
        NSString *personPhone = (NSString *)ABphone;

        NSMutableDictionary *dictToAdd = [[[NSMutableDictionary alloc]init]autorelease];
        if(firstName != nil && firstName != NULL){
            [dictToAdd setObject:firstName forKey:@"firstName"];
            CFRelease(firstName);
        }
        else{
            [dictToAdd setObject:@"" forKey:@"firstName"];
        }
        if(lastName != nil && lastName != NULL){
            [dictToAdd setObject:lastName forKey:@"lastName"];
            CFRelease(lastName);
        }
        else{
            [dictToAdd setObject:@"" forKey:@"lastName"];
        }

        if(personPhone != nil && personPhone != NULL){
            [dictToAdd setObject:personPhone forKey:@"mobile"];
            CFRelease(ABphone);
        }
        else{
            [dictToAdd setObject:@"" forKey:@"mobile"];
        }

        //Get the first name and last name added to dict and combine it to full name
        NSString *firstName = [dictToAdd objectForKey:@"firstName"];
        NSString *lastName = [dictToAdd objectForKey:@"lastName"];
        NSString *fullName = [firstName stringByAppendingString:lastName]; 

        //Now check whether the full name is same as your reminderToDisplay.Name
        if(reminderToDisplay.Name isEqualToString:fullName )
        {
            CFDataRef imageData = ABPersonCopyImageData(person);
            UIImage *image = [UIImage imageWithData:(NSData *)imageData];
            if(image != nil && image != NULL){
                [dictToAdd setObject:image forKey:@"image"];
                CFRelease(imageData);
            }
            else{
                [dictToAdd setObject:[UIImage imageNamed:TEMP_IMG] forKey:@"image"];
            }
        }

        [contactsToBeAdded addObject:dictToAdd];
    }

    CFRelease(phonelist);
}
CFRelease(allPeople);
CFRelease(addressbook);

[self.tableView reloadData];

numberOfRowsInSection

return contactsToBeAdded.count;

cellForRowAtIndexPath

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)
{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];

}

NSDictionary *contactToAdd;
//This way you can get the data added in viewDidLoad method
contactToAdd = [contactsToBeAdded objectAtIndex:indexPath.row];

NSString *fName = (NSString *)[contactToAdd objectForKey:@"firstName"];
NSString *lName = (NSString *)[contactToAdd objectForKey:@"lastName"];
UIImage *contactImg = (UIImage*)[contactToAdd objectForKey:@"image"];
于 2013-03-06T04:30:34.507 回答
0

当您使用“ABAddressBookCopyArrayOfAllPeople”时,您会在通讯录中获得一组人员。我相信他们是 ABPerson 的类型。

您现在可以像您一样遍历它们的列表。对于每条记录调用“ABPersonCopyImageData”,这将为您提供图像数据作为 CFDataRef。

记住 CFDataRef 是一个到 NSData 的无工具桥梁。

于 2013-02-28T15:25:57.513 回答
0

只需尝试像这样更改您的代码。您正在向 ur 添加字典项contactsList,因此获取每个字典并检查它是否包含与您匹配的键reminderToDisplay.Name,如果是,则执行您的操作。

for (NSDictionary* dict in contactsList)
{
   if(nil != [dict objectForKey:reminderToDisplay.Name])
   {
     //take image here
   }
}

更新:

//This is the reminder's name you want to get contact image
ReminderClass *reminderToDisplay = [self.remindersArray objectAtIndex:indexPath.row];

 //Here you are adding your contacts with full name as object and kName as key, but check ur kName here
 NSDictionary *contactsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:self.contactName, kName, [NSNumber numberWithInt:i], kIndex, nil]; 
 [self.contactsList addObject:contactsDictionary];

 //I dont think this part is needed in ur code
 NSDictionary *contactsDictionary = [self.contactsList objectAtIndex:indexPath.row]; 
 self.contactName = [contactsDictionary objectForKey:kName]; 
 int addressIndex = [[contactsDictionary objectForKey:kIndex]intValue];

现在,您将联系人姓名作为联系人列表数组中的字典,迭代数组并检查字典是否包含您的reminderToDisplay.Name 作为键。

for (NSDictionary* dict in contactsList)
{
    //Please note that your dict contains key as kName and object as contact name.
    if(nil != [dict objectForKey:reminderToDisplay.Name])
    {
    }
}

另外,我觉得您可以在一个循环中执行此操作,例如当您迭代地址簿本身时,您可以检查联系人姓名是否在您的提醒列表中,如果可用则提取图像。

希望这会有所帮助..一切顺利...

于 2013-03-01T08:39:08.063 回答