-1

我想按字母顺序在表格视图中显示电话联系人和图片。

我设法以正确的顺序显示联系人姓名,但无法以正确的顺序显示图像。

我已经尝试了几天但无法成功。

我知道如何对数组进行排序,但不知道如何对图像数据进行排序。

请帮帮我。

这是我的演示代码:

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

        });
    });

谢谢..

4

2 回答 2

1

答案很简单。

在您的类中添加一个Person存储NSData. 喜欢:

 @property (nonatomic, strong) NSData *imageData;

并使用:

person.imageData = imgdata;
于 2013-06-28T09:04:59.197 回答
0

我也面临同样的问题。我已经通过这样的关联对象解决了这个问题。

加载源数组时:

 objc_setAssociatedObject(hDetails, @"imageUrl", record, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

在 indexpath 处的行的单元格中:

Record *record = objc_getAssociatedObject(hDetails,@"imageUrl");

如需更多参考,请参阅:

如何在对象内使用 objc_setAssociatedObject/objc_getAssociatedObject?

于 2013-06-28T10:15:58.110 回答