2016-01-11 58 views
-1

我取iPhone的接觸,使他們的字典檢查我的代碼如何從表視圖傳遞一個字典數據到另一個視圖控制器

-(void) fetchContacts 
{ 

    CNContactStore *store = [[CNContactStore alloc] init]; 
    [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) { 
     if (granted == YES) { 
      //keys with fetching properties 
      NSArray *keys = @[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey, CNContactImageDataKey]; 
      NSString *containerId = store.defaultContainerIdentifier; 
      NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:containerId]; 
      NSError *error; 
      NSArray *cnContacts = [store unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error]; 
      if (error) { 
       NSLog(@"error fetching contacts %@", error); 
      } else { 
       NSString *phone; 
       NSString *fullName; 
       NSString *firstName; 
       NSString *lastName; 
       UIImage *profileImage; 
       NSMutableArray *contactNumbersArray; 
       for (CNContact *contact in cnContacts) { 
        // copy data to my custom Contacts class. 
        firstName = contact.givenName; 
        lastName = contact.familyName; 
        if (lastName == nil) { 
         fullName=[NSString stringWithFormat:@"%@",firstName]; 
        }else if (firstName == nil){ 
         fullName=[NSString stringWithFormat:@"%@",lastName]; 
        } 
        else{ 
         fullName=[NSString stringWithFormat:@"%@ %@",firstName,lastName]; 
        } 
        UIImage *image = [UIImage imageWithData:contact.imageData]; 
        if (image != nil) { 
         profileImage = image; 
        }else{ 
         profileImage = [UIImage imageNamed:@"acc_sett.png "]; 
        } 
        for (CNLabeledValue *label in contact.phoneNumbers) { 
         phone = [label.value stringValue]; 
         if ([phone length] > 0) { 
          [contactNumbersArray addObject:phone]; 
         } 
        } 
        NSDictionary* personDict = [[NSDictionary alloc] initWithObjectsAndKeys: fullName,@"fullName",profileImage,@"userImage",phone,@"PhoneNumbers", nil]; 
        [_Contacts addObject:personDict]; 

        NSLog(@"%@",phone); 
        NSLog(@"%@",fullName); 
       } 
       dispatch_async(dispatch_get_main_queue(), ^{ 
       [self.contacttableview reloadData]; 



       }); 
      } 
     } 
    }]; 

} 

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

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ 
    return 1; 
} 

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


    NSDictionary* personDict = [_Contacts objectAtIndex:indexPath.row]; 



    UITableViewCell *cell = nil; 
    cell = [_contacttableview dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath]; 





    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"]; 





    } 

    cell.imageView.image = [personDict objectForKey:@"userImage"]; 
    cell.textLabel.text = [personDict objectForKey:@"fullName"]; 
    cell.detailTextLabel.text = [personDict objectForKey:@"phoneNumbers"]; 
    return cell; 
} 

有了這個代碼字典數據的幫助是在顯示tableview現在我想通過其他視圖控制器上的人的圖像全名和電話號碼來選擇他們的行。

我已經嘗試了許多方法,但沒有得到任何結果

+0

您應該創建適當的類,即MyContact。這個數據模型應該填充你從聯繫簿中獲取的數據。這是第一個設計問題。爲了在類之間傳遞數據,有許多方法。您可以使用屬性並將它們設置爲segue方法。或者你可以使用委託方法等等 – NSNoob

回答

0

在YourViewController.h,創建要通過

@property (nonatomic, assign) NSDictionary *person; 

在TableViewController.m人的屬性,didSelectRowAtIndexPath方法:

YourViewController *vc = [[YourViewController alloc] init]; 
vc.person = [_Contacts objectAtIndex:indexPath.row]; 
[self.navigationController pushViewController:vc animated:YES]; 
+0

如何調用Dctionary的ViewController.m上的數據 –

+0

你可以調用viewDidLoad方法 –

+0

UIImage * userImage = [self.person objectForKey:@「userImage」]; NSString * fullName = [self.person objectForKey:@「fullName」]; NSString * phoneNumbers = [self.person objectForKey:@「phoneNumbers」]; –

0

首先在您要導航的OtherViewController上創建任何NSDictionary對象或您的模型類對象,然後編寫如下代碼,

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    OtherViewController *VC = [[OtherViewController alloc] init]; 
    VC.objContactModel = [_Contacts objectAtIndex:indexPath.row]; 
    [self.navigationController pushViewController:VC animated:YES]; 
} 

希望它能起作用。

+0

橡木那麼我必須做的 –

+0

你在使用模型類對象嗎? –

+0

在您想要導航的視圖控制器上創建對象,然後在當前的工作控制器中簡單地編寫上面的didSelect方法。 這裏「objContactModel」是您必須創建的模型類的對象。 –

相關問題