1

我正在尝试将 AddressBookUI API 集成到我的 iOS 5 应用程序中,以在表格视图中显示选定的内容。我已经能够实现 AddressBook Picker 并对其进行设置,以便当用户从他们的地址簿中选择一个人时,它会填充 TableView 中单元格的标签。我还希望它能够在同一单元格中显示所选人员的图像(如果存在),如果不存在则显示默认的缺失图片图像。

我无法理解如何将名称和图像数据存储在同一个 TableView 单元格中。

任何人都对我如何实现这一点有任何建议。我已阅读开发人员文档并且知道我应该使用 ABPersonCopyImageDataWithFormat 命令。我似乎无法将它实现到 tableview 单元格中。

以下是我到目前为止的代码片段:

// Store the name into memory when the user selects, then dismiss the view.
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{

    NSString *selectedPerson = (__bridge NSString *)ABRecordCopyCompositeName(person);


    [people insertObject:selectedPerson atIndex:0];

    if (ABPersonHasImageData(person) == TRUE) {
        NSLog(@"Person has an image!");
    } else {
        NSLog(@"Person does not have an image.");
    }

    [self dismissModalViewControllerAnimated:YES];

    [self.tableView reloadData];

    return NO;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PartyCell *cell = (PartyCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell"];
    cell.backgroundView = [[GradientView alloc] init];

    cell.personLabel.text = [people objectAtIndex:indexPath.row];
    cell.personImage.image = [UIImage imageNamed:@"missing.png"]; // THIS NEEDS TO DISPLAY THE SELECTED USERS PICTURE. CURRENTLY SHOWS A DEFAULT USER.

    return cell;
}

谢谢!

4

2 回答 2

3

找到了一个简单的解决方案:每次将所选联系人的图像存储在索引 0 处的新 NSMutableArray 中。

UIImage *image =[UIImage imageWithData:(__bridge NSData *)ABPersonCopyImageDataWithFormat(person, kABPersonImageFormatThumbnail)];

        [peopleImage insertObject:image atIndex:0];

然后可以通过调用 UITabelView 中的数组将图像正常加载到单元格中

cell.personImage.image = [peopleImage objectAtIndex:indexPath.row];
于 2012-01-06T01:28:13.097 回答
0

首先,您从 ABRecordRef 中获得一个 UIImage:

[UIImage imageWithData:(NSData *)ABPersonCopyImageDataWithFormat(person, kABPersonImageFormatThumbnail)];

然后,您可以设置 UITableViewCell 的 imageView 的图像,如:

[[cell imageView] setImage: image];

结合其他可能对您有帮助的链接:

http://www.chrisdanielson.com/tag/uitableviewcell/

http://goddess-gate.com/dc2/index.php/post/421

于 2012-01-04T00:03:44.347 回答