Abhishek Thapli Thapliyal Abhishek Thapli Thapliyal - 3 months ago 10
iOS Question

ios : UITableView is showing data when i tap on it

Hello i'm creating custom contact view using

UITableView
. I fetched all contacts and using table
relodData
but it not showing in table. On a single tap on table then data is visible. In view will appear i'm calling to fetch all contacts in phone and on completion it will reload
tableView
to make contact visible but it shows if i tap on it or open other view and come back.

#import "CMContatcsVC.h"
#import "CMContatcsTableCell.h"

@implementation CMContatcsVC

//======================================================================================================================================================
#pragma mark - VIEW LIFE CYCLE
//======================================================================================================================================================

- (void)viewDidLoad
{
[super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}

-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];

[self.tableView setDelegate:self];
[self.tableView setDataSource:self];

[self fetchAllContactsCompletionHandler:^(BOOL granted, NSError * _Nullable error) {


[self.tableView reloadData];
NSLog(@"COUNT_IN_COMPLETION_HANDLER :: %lu", (unsigned long)self.contactArray.count);
}];
}

-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}

//======================================================================================================================================================
#pragma mark - TABLE VIEW DELEGATES METHODS
//======================================================================================================================================================

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSLog(@"TABLE SECTION");
return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"TABLE ROWS :: %lu",(unsigned long)self.contactArray.count);
return self.contactArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"TABLE CELL FPR ROW");
CMContatcsTableCell *contactCell = (CMContatcsTableCell *)[tableView dequeueReusableCellWithIdentifier:@"CMContatcsTableCell" forIndexPath:indexPath];

@try
{
CMCustomContacts * contact = [self.contactArray objectAtIndex:indexPath.row];
[contactCell.profileImgView setImage:contact.profileImage];
[contactCell.nameLabel setText:contact.firstName];
NSLog(@"TABLE CELL CNT NAME :: %@", contact.firstName);
[contactCell.contactLabel setText:[contact.phoneArray objectAtIndex:0]];
}
@catch (NSException *exception)
{
NSLog(@"TABLE CELL EXCEPTION :: %@", exception.description);
}
@finally
{}

return contactCell;
}

//======================================================================================================================================================
#pragma mark - FETCH ALL CONTACTS
//======================================================================================================================================================

-(void)fetchAllContactsCompletionHandler:(void(^)(BOOL granted, NSError * _Nullable error))completion
{
self.contactArray = [[NSMutableArray alloc] init];

CNContactStore *store = [[CNContactStore alloc] init];
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {

if (granted)
{
NSArray *keys = @[CNContactNamePrefixKey, CNContactFamilyNameKey, CNContactGivenNameKey,
CNContactPhoneNumbersKey, CNContactImageDataKey, CNContactEmailAddressesKey];

NSString *containerId = store.defaultContainerIdentifier;
NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:containerId];
NSError *error;
NSArray *cnContacts = [store unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error];

if (error)
{
NSLog(@"ERROR IN FETCHING CONTACTS :: %@", error.description);
}
else
{
for (CNContact *contact in cnContacts)
{
@try
{
CMCustomContacts *newContact = [[CMCustomContacts alloc] init];
newContact.phoneArray = [[NSMutableArray alloc] init];
newContact.emailArray = [[NSMutableArray alloc] init];

newContact.firstName = contact.givenName;
newContact.lastName = contact.familyName;

UIImage *image = [UIImage imageWithData:contact.imageData];
newContact.profileImage = image;

for (CNLabeledValue *label in contact.phoneNumbers)
{
NSString *phone = [label.value stringValue];
if ([phone length] > 0)
{
[newContact.phoneArray addObject:phone]; NSLog(@"PHONE :: %@",phone);
}
}

for (CNLabeledValue *label in contact.emailAddresses)
{
NSString *email = label.value;
if ([email length] > 0)
{
[newContact.emailArray addObject:email]; NSLog(@"EMAIL :: %@",email);
}
}

[self.contactArray addObject:newContact];
}
@catch (NSException *exception)
{
NSLog(@"EXCEPTION IN CONTACTS :: %@", exception.description);
}
@finally
{
NSLog(@"FINALLY");
}
}

NSLog(@"COUNT OF CONTACTS :: %lu", (unsigned long)self.contactArray.count);
}
}
completion(granted, error);
}];
}

@end

Answer

Always update your UI on main thread, so call reloadData of TableView on main thread like this.

[self fetchAllContactsCompletionHandler:^(BOOL granted, NSError * _Nullable error) {

    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
    }        
    NSLog(@"COUNT_IN_COMPLETION_HANDLER :: %lu", (unsigned long)self.contactArray.count);
}];
Comments