1

I have a array which stores all the contacts from the phone. I have a UITableView in which i want to display all the contacts. I was having trouble displaying them in the table view o i created a new array, and used the below code to loop through the contacts array and fetch strings from it and store them in the new array (contactsNew)

for (CNContact *contact in contacts)
    {
        NSString *string = [formatter stringFromContact:contact];
        [contactsNew addObject:string];
        NSLog(@"contact = %@", string);
    }

The issue is all the contacts are displayed in the log, but i am still not able to display the contacts in table view.

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

- (UITableViewCell *)tableView:(UITableView *)tableView    
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *simpleTableIdentifier = @"SimpleTableCell";

UITableViewCell *cell = [tableView   
dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc]   
initWithStyle:UITableViewCellStyleDefault   
reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [contactsNew objectAtIndex:indexPath.row];
return cell;
}

I have checked the values of 'string' variable and the number and content being added to the contactsNew array, and everything looks fine.

Also can i use something else in place of:

cell.textLabel.text = [contactsNew objectAtIndex:indexPath.row];

So that i can directly use the contacts array instead of creating a new array. Thanks in advance.

Tejas K
  • 682
  • 11
  • 28

5 Answers5

1
Try this

for (CNContact *contact in contacts)
{
        NSString *string = [formatter stringFromContact:contact];
        [contactsNew addObject:string];
        NSLog(@"contact = %@", string);
}
[tableview reloadData];
Mayank Patel
  • 3,868
  • 10
  • 36
  • 59
Yagnesh Dobariya
  • 2,241
  • 19
  • 29
1

You need to reload tableView after creating array so use [tableView reloadData] after for loop

for (CNContact *contact in contacts)
{
    NSString *string = [formatter stringFromContact:contact];
    [contactsNew addObject:string];
    NSLog(@"contact = %@", string);
}

[tableview reloadData]; //Add this line

Hope this will works.

1

you need to reload tableview after some delay. use like this

 for (CNContact *contact in contacts)
{
    NSString *string = [formatter stringFromContact:contact];
    [contactsNew addObject:string];
    NSLog(@"contact = %@", string);
}

//  add after Delay 0.5 if its not worked then 1.0

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    [tableview reloadData];
});
Vvk
  • 4,031
  • 29
  • 51
1

you have to reload your table on main thread after you fetch the contacts... like:- dispatch_async(dispatch_get_main_queue(), ^{ [_table reloadData]; });

...and for your table view write

cell.textLabel.text = [contactsNew objectAtIndex:indexPath.row]valueForKey:@"keys";

...provide the key here.

CinCout
  • 9,486
  • 12
  • 49
  • 67
Amrut Gaikwad
  • 59
  • 2
  • 11
0

Here are few points you need to check to resolve your issue :

  • First thing you need to check is weather tableview methods called once after you added values to new Array ? If no than try reload table view after you are adding values to new array.
  • If it calls correctly after adding values to new array try to NSLog values in cellForRowAtIndexPath method

Hope it will help you.

Mayur Prajapati
  • 5,454
  • 7
  • 41
  • 70