我在UITableView
上創建了ABAddressBook
。現在,我需要將firstname
,last 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
行,我想通過您的聯繫方式,以一個控制器。請幫忙?
[查看控制器之間傳遞數據(HTTP的可能重複。 com/questions/5210535/passing-data-between-view-controllers) – mgyky
我需要像這個例子一樣的cellForRowAtIndexPAth實現http://www.appcoda.com/search-bar-tutorial-ios7/ –