如何使用A Record Get RecordID来填充包含联系信息的表格

时间:2014-05-21 13:20:25

标签: ios xcode sqlite nsnumber

我有一个sqlite数据库,我存储了联系人的ABRecordGetRecordID。 我将ABRecordGetRecordID转换为NSNumber以将其存储在数据库中:

2014-05-21 14:35:12.122 ProjectMobile [17858:60b] 179962464< - 这是我在记录以下内容时得到的数字:

#import "HulpViewcontroller.h"
#import "AidTableViewcell.h"
@property UIColor * firstColor;
@property UIColor * secondColor;
@property NSMutableArray *Personen;
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@end

@implementation HulpViewController

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.Personen count];
}

- (IBAction)pickPerson:(id)sender {
    ABPeoplePickerNavigationController *picker =
    [[ABPeoplePickerNavigationController alloc] init];
    picker.peoplePickerDelegate = self;

    [self presentViewController:picker animated:YES completion:nil];
}


- (void)peoplePickerNavigationControllerDidCancel: (ABPeoplePickerNavigationController *)peoplePicker {

    [[peoplePicker presentingViewController] dismissViewControllerAnimated:YES completion:nil];
}


- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {

    // Create a new person
    NSManagedObject *newPersoon = [NSEntityDescription insertNewObjectForEntityForName:@"Personen" inManagedObjectContext:self.context];
    NSNumber *id = [NSNumber numberWithInt:ABRecordGetRecordID(person)];
    NSNumber *one = [NSNumber numberWithInt:1];
    NSLog(@"%d", id);
    [newPersoon setValue: id forKey:@"persoonId"];
    [newPersoon setValue: one forKey:@"importance"];

    NSError *error = nil;
    // Save the object to persistent store
    if (![self.context save:&error]) {
        NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
    }




        [[peoplePicker presentingViewController] dismissViewControllerAnimated:YES completion:nil];
    return NO;
}

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
      shouldContinueAfterSelectingPerson:(ABRecordRef)person
                                property:(ABPropertyID)property
                              identifier:(ABMultiValueIdentifier)identifier
{

    return NO;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"hulpCell";

    AidTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[AidTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }


    // check if row is odd or even and set color accordingly
    if (indexPath.row % 2) {
        cell.backgroundColor = self.firstColor;
    }else {
        cell.backgroundColor = self.secondColor;
    }

    return cell;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.firstColor = [UIColor colorWithRed:183/255.0f green:80/255.0f blue:23/255.0f alpha:1.0f];
    self.secondColor = [UIColor colorWithRed:179/255.0f green:98/255.0f blue:0/255.0f alpha:1.0f];
    self.Personen = [[NSMutableArray alloc]initWithObjects:@"Man.jpeg", @"Doctor.jpeg", @"Son.jpeg", nil];

    // Fetch the devices from persistent data store
    NSManagedObjectContext *managedObjectContext = [self context];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Personen"];
    self.personen = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];

    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

我如何使用存储在数据库中的Id来将这些id的联系人信息放在tableview中?

我更新了代码。这样你们就会更容易帮助我:)

谢谢!

1 个答案:

答案 0 :(得分:0)

你可以,例如做这样的事情:

CFErrorRef error = nil;
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions (NULL, &error);

if (addressBookRef != nil) {
    ABRecordID recordID = yourPersonRecordID; // Assign here your ID
    ABRecordRef nxtABRecordRef = ABAddressBookGetPersonWithRecordID (addressBookRef, recordID);
    // Do anything with your record, e.g.
    ABMultiValueRef addressesRef = ABRecordCopyValue(nxtABRecordRef, kABPersonAddressProperty);

    if (addressesRef != nil) {
        for (int index = 0; index < ABMultiValueGetCount(addressesRef); index++) {
            NSDictionary *addressDictionary = (__bridge_transfer NSDictionary*) ABMultiValueCopyValueAtIndex(addressesRef,index);
            // Do something with the persons data
        } // for all addresses

        CFRelease(addressesRef);
    }

    CFRelease(addressBookRef);
} else {
    NSLog(@"Could not open address book");
}