您好我使用UITableView
创建自定义联系人视图。我获取了所有联系人并使用了表relodData
,但未在表格中显示。只需点击一下表,即可看到数据。在视图中,我会打电话来取回手机中的所有联系人,完成后它会重新加载tableView
以使联系人可见,但它显示我是点击它还是打开其他视图然后回来。
#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
答案 0 :(得分:4)
始终更新主^AB.*\/$
^ - matches the start of the string
AB - matches the string 'AB' exactly
.* - matches any character ('.') any number of times ('*')
\/ - matches the literal character '/' (backslash is the escape character)
$ - matches the end of the string
上的UI
,因此请在主thread
上致电reloadData
TableView
。
thread