Populating table with contacts from address book in Xcode 4.3
I am making an app for iPad and iPhone. on the first screen you need to login, it works. but then I come to the next screen which is a table. I want this view to be first name (first and last name) from adresbook on iPad or iPhone.
I am getting a table view, but it is empty. can anyone help me?
this is my controller (.m, I don't need to edit .h):
#import "TableViewController.h"
#import "ViewController.h"
#import "AddressBook/AddressBook.h"
@interface TableViewController ()
@end
@implementation TableViewController
{
NSMutableArray *ListOfItems;
}
@synthesize tblContacts;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[self ListContacts];
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)viewDidUnload
{
[self setTblContacts:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)Back:(id)sender{
[self dismissModalViewControllerAnimated:YES];
}
- (void)ListContacts{
/*ABAddressBookRef addressBook = ABAddressBookCreate( );
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople( addressBook );
CFIndex nPeople = ABAddressBookGetPersonCount( addressBook );
for ( int i = 0; i < nPeople; i++ )
{
ABRecordRef ref = CFArrayGetValueAtIndex( allPeople, i );
UITableView ContactList
} */
ListOfItems = [[NSMutableArray alloc] init];
[ListOfItems addObject:@"Iceland"];
[ListOfItems addObject:@"Greenland"];
[ListOfItems addObject:@"Switzerland"];
[ListOfItems addObject:@"Norway"];
[ListOfItems addObject:@"New Zealand"];
[ListOfItems addObject:@"Greece"];
[ListOfItems addObject:@"Rome"];
[ListOfItems addObject:@"Ireland"];
self.navigationItem.title = @"Countries";
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [ListOfItems count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
}
// Set up the cell...
NSString *cellValue = [ListOfItems objectAtIndex:indexPath.row];
cell.text = cellValue;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
@end
note that I tried to populate my table with some hardcoded values. but again, my desk remains empty. Please, help.
source to share
#import <UIKit/UIKit.h>
#import <AddressBook/AddressBook.h>
@interface AddressBookViewController: UIViewController <ABPeoplePickerNavigationControllerDelegate> {}
-(IBAction) btnClicked:(id)sender;
@end
It is an address book api for accessing the contacts app from your own app. Maybe you can use it and it will be much faster than implementing your own UITableView and that's it.
Hope it helps.
source to share
Have you installed the table delegate and data source? You can do this easily through Interface Builder:
- Select the table view in Interface Builder.
- Navigate to the Connector Inspector in the right pane (the last tab where you link outputs and actions).
- Bind a data source and delegate outputs to a file owner (or a file with information on how to control the table view).
Don't forget to include your own class in the file owner id pointer (again, in the right pane of Xcode, third tab).
Hope this helps you!
source to share