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 oi 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.
Try this
for (CNContact *contact in contacts)
{
NSString *string = [formatter stringFromContact:contact];
[contactsNew addObject:string];
NSLog(@"contact = %@", string);
}
[tableview reloadData];
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.
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];
});
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.
Here are few points you need to check to resolve your issue :
NSLog
values in cellForRowAtIndexPath
method Hope it will help you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.