我想按字母順序顯示手機聯繫人以及桌面視圖中的圖片。在桌面視圖中顯示聯繫人圖像以及聯繫人姓名
我設法以正確的順序顯示聯繫人姓名,但無法以正確的順序顯示圖像。
我一直在嘗試這些幾天,但不能成功。
我知道如何排序數組,但不知道如何排序圖像數據。
請幫幫我吧。
這裏是我的演示代碼:用於顯示在細胞圖像
NSArray *sortedFirstNames;
- (void)getPersonDetails
{
CFErrorRef error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
if (addressBook != nil)
{
NSLog(@"Succesful.");
NSArray *allContacts = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
NSUInteger i = 0;
NSMutableArray *firstNames = [[NSMutableArray alloc] init];
NSMutableArray *firstimages = [[NSMutableArray alloc] init];
for (i = 0; i < [allContacts count]; i++)
{
Person *person = [[Person alloc] init];
ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];
NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
NSString *lastName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);
NSString *fullName;
if (lastName == nil)
{
fullName = [NSString stringWithFormat:@"%@", firstName];
[firstNames addObject: fullName];
}
else if(firstName ==nil)
{
fullName = [NSString stringWithFormat:@"%@", lastName];
[firstNames addObject: fullName];
}
else
{
fullName = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
[firstNames addObject: fullName];
}
person.firstName = firstName;
person.lastName = lastName;
person.fullName = fullName;
//email
imgdata=(__bridge NSData*)ABPersonCopyImageDataWithFormat(contactPerson, kABPersonImageFormatThumbnail);
NSLog(@"newstris%@",imageStr);
if(imgdata==nil)
{
}
else
{
[firstimages addObject:imgdata];
}
[self.tableData addObject:person];
}
sortedFirstNames = [firstNames sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
}
CFRelease(addressBook);
}
代碼:
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
UIImage *image1;
// cell.imageView.frame = CGRectMake(0, 0, 50, 50);
if (person.imgdata==nil) {
image1=[UIImage imageNamed:@"default_profile_pic.jpg"];
}
else
{
image1 = [UIImage imageWithData:person.imgdata];
}
UIImageView *imageView = [[UIImageView alloc] initWithImage:image1];
imageView.frame = CGRectMake(6.5, 6.5, 45., 45.);
dispatch_sync(dispatch_get_main_queue(), ^{
[cell addSubview:imageView];
});
});
謝謝..
感謝您的回答,我已經做到了,但圖像沒有顯示相關的名稱。我想要圖像以及那裏的名稱在阿爾法(A-Z) –
您確定圖像數據在cellForRowAtIndexPath中不爲空嗎? –
@VarunVishnoi:你使用過財產嗎? –