장치의 모든 연락처 이름을 표시하는 테이블보기가 있습니다. 이 이름은 contactsArray
이라는 배열에서옵니다. 각 contact
객체에 대해 phoneNumbers
객체를 얻고 숫자를 phoneNumberArray
이라는 다른 배열로 가져옵니다. 나는 그것들을 내 테이블 뷰에 넣을 때 각각의 접촉을 해당 번호와 함께 표시합니다 ... 그러나 오랫동안 만. 몇개의 행이 내려 갔을 때, 일부 contact
개체에 복수 전화 번호가있는 phoneNumbers
개체가 있었기 때문에 더 이상 올바른 연락처와 일치하지 않습니다. 연락처와 전화 번호가 같아 지도록 각 객체의 첫 번째 전화 번호 만 가져올 수 있습니까?기기의 모든 연락처에서 첫 번째 전화 번호 만 가져 오는 방법은 무엇입니까?
@property (nonatomic, strong) NSMutableArray *contactsArray;
@property (nonatomic, strong) NSMutableArray *phoneNumberArray;
@end
@implementation Contacts
- (void)viewDidLoad
{
[super viewDidLoad];
self.phoneNumberArray = [[NSMutableArray alloc]init];
self.contactsArray = [[NSMutableArray alloc]init];
[self fetchContactsandAuthorization];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.contactsArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId = @"contactCell";
ContactCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
CNContact *contact = self.contactsArray[indexPath.row];
NSString *phone = self.phoneNumberArray[indexPath.row];
NSString *contactName = [NSString stringWithFormat:@"%@ %@",contact.givenName,contact.familyName];
NSString *contactNumber = phone;
cell.name.text = contactName;
cell.number.text = contactNumber;
[cell.inviteBtn addTarget:self action:@selector(openMessagesForContact:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 72;
}
-(void)fetchContactsandAuthorization
{
// Request authorization to Contacts
CNContactStore *store = [[CNContactStore alloc] init];
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted == YES)
{
CNContactStore *addressBook = [[CNContactStore alloc]init];
NSArray *keysToFetch [email protected][CNContactFamilyNameKey,
CNContactGivenNameKey,
CNContactPhoneNumbersKey];
CNContactFetchRequest *fetchRequest = [[CNContactFetchRequest alloc]initWithKeysToFetch:keysToFetch];
[addressBook enumerateContactsWithFetchRequest:fetchRequest error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) {
[self.contactsArray addObject:contact];
NSString *phone;
for (CNLabeledValue *label in contact.phoneNumbers) {
phone = [label.value stringValue];
if ([phone length] > 0) {
[self.phoneNumberArray addObject:phone];
}
}
}];
dispatch_async(dispatch_get_main_queue(), ^{
[self.contactsTableView reloadData];
});
}
}];
}
@end
답이 대답을 –
@simon_smiley 감사로 받아 들일 수 있도록 귀하의 질문을 해결했는지 잊지 마십시오. Simon. 나는 이번 주 결승을 끝내고 있기 때문에 아직 시도하지 않았다. 내 질문에 대한 훌륭한 대답 인 것 같습니다. –