2012-01-03 14 views
1

我正在嘗試將AddressBookUI API集成到我的iOS 5應用程序中,以便在表格視圖中顯示選定的內容。我已經能夠實現AddressBook Picker,並在用戶從地址簿中選擇一個人時設置它,它將在TableView中填充單元格的標籤。我還希望它能夠顯示選定人員在同一單元格中的圖像(如果存在)以及缺少圖像(如果沒有圖像)。在UITableViewCell中設置iOS通訊錄圖片

我不明白如何將名稱和圖像數據存儲在同一個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; 
} 

謝謝!

回答

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]; 
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

+0

我曾嘗試的這兩行代碼,但它不是爲我工作。我用我正在使用的兩段代碼編輯了我的原始帖子。 – Brian 2012-01-04 03:02:48