Search code examples
iostableviewaddressbook

UITableView doesn't display Address Book


I think I'm not getting the handle on address book and UITableView, or maybe I'm just making it simpler than it is.

here is my .h file:

@interface Contacts : UITableViewController <ABPeoplePickerNavigationControllerDelegate> {

NSMutableArray *menuArray;
NSString *firstName;
NSString *lastName;
UIButton *addcontact;
}

@property (nonatomic, retain) NSMutableArray *menuArray;
@property (nonatomic, strong) UIButton *addcontact;
@property (nonatomic, strong) NSString *firstName;
@property (nonatomic, strong) NSString *lastName;

-(void)showPeoplePickerController;
@end

And my .m file:

 -(IBAction)addcontact:(id)sender {

ABPeoplePickerNavigationController *peoplePicker=[[ABPeoplePickerNavigationController alloc] init];

[peoplePicker setPeoplePickerDelegate:self];



[self presentModalViewController:peoplePicker animated:YES];  }
- (void)viewDidLoad
{
menuArray = [[NSMutableArray alloc] init];

[super viewDidLoad];

} 

- (BOOL)peoplePickerNavigationController:

(ABPeoplePickerNavigationController *)peoplePicker

  shouldContinueAfterSelectingPerson:(ABRecordRef)person {

    firstName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
    [menuArray addObject:firstName]; 
    lastName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
    [menuArray addObject:lastName];



[self dismissModalViewControllerAnimated:YES];
return NO;}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

return [menuArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

NSString *cellValue = [menuArray objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;

return cell;

}

I hope someone could help me with it, I'm fairley new to UITableViews and Addressbooks :)


Solution

  • For performance purposes (not to mention animation purposes), UITableView does not automatically update itself whenever the data source changes.

    Try adding [myTableView reloadData]; after you add the first and last name to the arrays (or in viewWillAppear: