I am trying to integrate the AddressBookUI API into my iOS 5 app to display selected content in a table view. I have been able to implement the AddressBook Picker and set it so when a user selects a person from their address book, it populates the label of the cell in the TableView. I would also like it to be able to display the image of the selected person in the same cell if one exists and a default missing picture image if there is not one.
I can’t see to get my head around how to store both the name and the image data in the same TableView cell.
Anyone have any suggestions of how I might accomplish this. I have read the developer docs and know i should be using the ABPersonCopyImageDataWithFormat command. I just can’t seem to get it to implement into the tableview cell.
Here are the snippets of code I have so far:
// 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;
}
Thanks!
Found a simple solution: store the image of the selected contact in a new NSMutableArray at index 0 each time.
The images can then be loaded like normal into a cell by calling the array in the UITabelView