How do I get the contacts of users and present them in a UITableView?

Forgive my ignorance, but I have mainly done game programming and not as much as applications. What I am trying to do is get the "Users" contacts and present them as a table when the application starts. I'm not a complete noob, I know my way through the address book API. I just don't know how to accomplish the task of getting user contacts in my own UITableView. Any help is greatly appreciated!


source to share

1 answer

I'm not sure how good my answer is, because I'm not using the address book API, but a bit of searching + my own knowledge suggests this:

Create an address book

One way to do this (based on my searches) is as follows:

ABAddressBookRef addressBook = ABAddressBookCreate( );
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople( addressBook );
CFIndex nPeople = ABAddressBookGetPersonCount( addressBook );

for ( int i = 0; i < nPeople; i++ )
    ABRecordRef ref = CFArrayGetValueAtIndex( allPeople, i );


Display address book

Using the code above, you now need to display it inside your table. I don't know how familiar you are with the usage UITableView

, so I'll be more detailed than what I probably need. You are going to populate the following methods of the tableView class:

- (NSInteger)numberOfSections
    //You can also return 26 here, but you'll need to split your addressBook array, so each section contains the people starting with a single letter of the alphabet
    return 1;

- (NSInteger)numberOfRowsInSection:(NSInteger)section
    return [addressBook count];

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
    //do standard cell instantiation
    //get the contact at index indexPath.row
    //set the cell textLabel.text to that contact name (or whatever you want)


I don't know how helpful my answer is, but if you would like me to clarify a thing or two (or tell me that I'm stupid, thinking your problem is so simple), feel free to let me know.



All Articles