2015-06-22 45 views
-1

我在UITableView上創建了ABAddressBook。現在,我需要將firstnamelast name等傳遞給下一個視圖控制器。如何在兩個控制器之間傳遞數據,其中一個控制器在uitableviw中獲取abadressbook

我必須使用nsobject類傳遞數據...在這個項目中,我已經使Person類具有名稱,姓氏,數字,電子郵件的字符串屬性。

地址簿中的代碼是:

ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook); 
allPeople = (__bridge NSMutableArray *)(ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonFirstNameProperty)); 
NSInteger numberOfPeople = [allPeople count]; 
for (NSUInteger i = 0; i < numberOfPeople; i++) { 
    personContact = [[Person alloc]init]; 

    person = (__bridge ABRecordRef)allPeople[i]; 

    NSString *firstName = (__bridge NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty); 
    NSString *lastName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty)); 
    if (firstName == nil) { 
     firstName = @""; 
    } 
    if (lastName == nil) 
    { 
     lastName = @""; 
    } 

    NSString *fullName = [NSString stringWithFormat:@"%@ %@",firstName,lastName]; 

    personContact.firstName = firstName; 
    personContact.lastName = lastName; 
    personContact.fullName = fullName; 

//For adding multiple contacts: 



ABMultiValueRef phoneNumbers = ABRecordCopyValue((person), kABPersonPhoneProperty); 

    CFIndex numberOfPhoneNumbers = ABMultiValueGetCount(phoneNumbers); 
    for (CFIndex i = 0; i < numberOfPhoneNumbers; i++) { 
     NSString *phoneNumber = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phoneNumbers, i)); 

     if ([phoneNumber isEqualToString:@""]) 
     { 
      phoneNumber = @"Not Available"; 
     } 

     NSCharacterSet *trim = [NSCharacterSet characterSetWithCharactersInString:@"#();$&-+"]; 
     phoneNumber = [[phoneNumber componentsSeparatedByCharactersInSet: trim] componentsJoinedByString: @""]; 
     phoneNumber= [phoneNumber stringByReplacingOccurrencesOfString:@"\"" withString:@""]; 
     phoneNumber=[phoneNumber stringByReplacingOccurrencesOfString:@"" withString:@""]; 

     personContact.phoneNumber = phoneNumber; 

//Emails 

     ABMutableMultiValueRef eMail = ABRecordCopyValue(person, kABPersonEmailProperty); 
     if(ABMultiValueGetCount(eMail) > 0) 
     { 
      email =CFBridgingRelease(ABMultiValueCopyValueAtIndex(eMail, 0)); 
     } 
     else 
     { 
      email = @""; 
     } 
     personContact.email = email; 
    } 

//Photos 

    CFDataRef imgData = ABPersonCopyImageData(person); 
    NSData *imageData = (__bridge NSData *)(imgData); 

    phoneImage = [UIImage imageWithData:imageData]; 
    if (phoneImage == nil) { 
     phoneImage = [UIImage imageNamed:@"userEdit"]; 
    } 
    CGSize destinationSize = CGSizeMake(70, 70); 
    UIGraphicsBeginImageContext(destinationSize); 
    [phoneImage drawInRect:CGRectMake(0, 0, destinationSize.width, destinationSize.height)]; 

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    if (imgData != NULL) 
    { 
     CFRelease(imgData); 
    } 
    personContact.photos = (NSString *)newImage; 

    [contactsData addObject:personContact]; 
    [contactstableView reloadData]; 

} 
} 


And the code for cellforrowatIndex path is as follows:- 
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *[email protected]"CellID"; 
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
if (cell==nil) 
{ 

    cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; 
    cell.backgroundColor=[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:239.0/255.0 alpha:1.0]; 
} 


if (tableView == self.searchDisplayController.searchResultsTableView) { 
    personContact = [searchResults objectAtIndex:indexPath.row]; 

} 
else { 
    personContact = [contactsData objectAtIndex:indexPath.row]; 

} 
if (contactsData.count > 0) 
{ 
    cell.textLabel.text = personContact.fullName; 

    cell.imageView.image = personContact.photos; 
    cell.imageView.layer.cornerRadius = 35; 
    cell.imageView.layer.masksToBounds = YES; 
    cell.imageView.contentMode = UIViewContentModeScaleAspectFit; 

    } 

didselect行,我想通過您的聯繫方式,以一個控制器。請幫忙?

+0

[查看控制器之間傳遞數據(HTTP的可能重複。 com/questions/5210535/passing-data-between-view-controllers) – mgyky

+0

我需要像這個例子一樣的cellForRowAtIndexPAth實現http://www.appcoda.com/search-bar-tutorial-ios7/ –

回答

1

如果你不得不從ABAddressBook把它傳遞給視圖控制器使用委託方法

如果你要通過它來ABAddressBook你可以通過ABPerson或關聯性

objc_setAssociatedObject(actionSheet, @"selectedUserJidStr", selectedUserJidStr, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 
+0

我已經在我的uitableview中獲取數據,如上所示,現在我需要傳遞單元格的數據@ankit –

+1

personContact = [contactsData objectAtIndex:indexPath.row]; –

+0

我需要cellForRowAtIndexPAth相同的實現像這個例子appcoda.com/search-bar-tutorial-ios7 @ankit –

0

我猜你會顯示查看單元格選擇,你可以使用下面的委託方法。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [self performSegueWithIdentifier:@"segueName" sender:indexPath]; 
} 

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if([segue.identifier isEqualToString:@"segueName"]) 
    { 
     NSIndexPath *indexPath = sender; 
     ViewControllerName *objectOfViewControllerName = segue.destinationViewController; 
     objectOfViewControllerName.personContact = [contactsData objectAtIndex:indexPath.row]; 
    } 
} 
0

我在的cellForRowAtIndexPath

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
    AddGuestVC *add = [[AddGuestVC alloc]initWithNibName:@"AddGuestVC" bundle:nil]; 

    add.personContact = [contactsData objectAtIndex:indexpath.row]; 

    add.delegate = self; 
    add.personContact = personContact; 
    [self.navigationController pushViewController:add animated:YES]; 
} 
} 

這樣做的,用於填充文本框使用://計算器:

firstname.text = self.personContact.firstName; 
相關問題