我想要的是使用属性形成一个现有的CNContact
对象的子接触。 我从CNContactPickerViewController
得到CNContact
对象,并在tableview中显示所有属性。存储单元从桌面开关打开
tableviewcell嵌入了开关。我在导航项上有一个预览按钮,当按下按钮时,如果属性中的开关处于打开状态,则此属性应存储在新的CNMutableContact
中。
我的问题是:如果联系人有太多的属性,我无法获得存储的屏幕外属性。有没有办法解决这个问题。代码
部分获得子接触:
+(CNMutableContact*)newContactWithSelectedFieldInTableView:(UITableView*)tableView FromContact:(CNContact*)contact
{ CNMutableContact* aContact = [[CNMutableContact alloc]init];
//get all indexPath from tableview
NSMutableArray* indexPathArr = [[NSMutableArray alloc]init];
NSInteger nSections = [tableView numberOfSections];
for (int j=0; j<nSections; j++) {
NSInteger nRows = [tableView numberOfRowsInSection:j];
for (int i=0; i<nRows; i++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:j];
[indexPathArr addObject:indexPath];
}
}
//selected phone numbers
//go through indexPath
for (NSIndexPath* path in indexPathArr)
{
UITableViewCell* nameCell = [tableView cellForRowAtIndexPath:path];
UISwitch *mySwitch = (UISwitch *)nameCell.accessoryView;
switch (path.section)
{
case basicInfoSection://basic info section (name,company,department,title)
{
int row = 0;
if(path.row==row)
{
if(mySwitch.on)
{
aContact.givenName = contact.givenName;
aContact.middleName = contact.middleName;
aContact.familyName = contact.familyName;
}
}
if(![contact.organizationName isEqualToString:@""])
{row += 1;
if(path.row==row)//company row
{
//store company
if(mySwitch.on)
aContact.organizationName = contact.organizationName;
}
}
if(![contact.departmentName isEqualToString:@""])
{row += 1;
if(path.row==row)//department row
{
//store department
if(mySwitch.on)
aContact.departmentName = contact.departmentName;
}
}
if(![contact.jobTitle isEqualToString:@""])
{row += 1;
if(path.row==row)//jobTitle row
{
//store job Title
if(mySwitch.on)
aContact.jobTitle = contact.jobTitle;
}
}
}
break;
case phoneSection:
{
if(mySwitch.on)
{
aContact.phoneNumbers = [aContact.phoneNumbers arrayByAddingObject:contact.phoneNumbers[path.row]];
}
}
break;
我想出了一个解决方案,现在它做工精细,一些代码列如下:
1#创建UISwitch的子类
#import <UIKit/UIKit.h>
@interface SwitchWithIndex : UISwitch
@property (strong ,nonatomic) NSIndexPath* indexPath;
@end
2#创建的字典,与开关状态和在观看记录indexPath没有负载,用于根据所述需求的tableview布局所有可能indexPath循环数据源(在CNContact)。
for(NSInteger section=0; section<7; section++)
{
switch (section) {
{
//section 1 basic info(name, company, department, job title)
case basicInfoSection:
{int row =0;
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:row inSection:section];
[_switchStateAtIndex setObject:boolNumber forKey:indexPath];
if(![_contact.organizationName isEqualToString:@""])
{row += 1;
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:row inSection:section];
[_switchStateAtIndex setObject:boolNumber forKey:indexPath];
}
if(![_contact.departmentName isEqualToString:@""])
{row += 1;
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:row inSection:section];
[_switchStateAtIndex setObject:boolNumber forKey:indexPath];
}
if(![_contact.jobTitle isEqualToString:@""])
{row += 1;
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:row inSection:section];
[_switchStateAtIndex setObject:boolNumber forKey:indexPath];
}
}
break;
//section 2 phones
case phoneSection:
{
for(NSInteger row=0; row<[_contact.phoneNumbers count];row++)
{
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:row inSection:section];
[_switchStateAtIndex setObject:boolNumber forKey:indexPath];
}
}
break;
//more code ..
3#在表视图数据源代表加载开关的状态在indexpath和
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
SwitchWithIndex* mySwitch = [[SwitchWithIndex alloc] init];
[mySwitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
NSNumber* switchStateNumber =[_switchStateAtIndex objectForKey:indexPath];
BOOL switchState = [switchStateNumber boolValue];
mySwitch.on = switchState;
mySwitch.indexPath = [[NSIndexPath alloc]init];
mySwitch.indexPath = indexPath;
cell.accessoryView = mySwitch;
//more code
和下面的代码来检测开关状态改变。
- (void) switchChanged:(id)sender {
SwitchWithIndex* mySwitch = sender;
NSIndexPath* indexPath = mySwitch.indexPath;
NSLog(@"%@",indexPath);
NSNumber* switchStateBool = [NSNumber numberWithBool:mySwitch.on ? YES : NO];
[_switchStateAtIndex setObject:switchStateBool forKey:indexPath];
NSLog(@"The switch is %@", mySwitch.on ? @"ON" : @"OFF");
}
4#,基于开关状态好不容易攒选定字段
+(CNMutableContact*)newContactFrom:(CNContact*)contact withSwitchState:(NSMutableDictionary*)switchState
{
CNMutableContact* aContact = [[CNMutableContact alloc]init];
for (NSIndexPath* indexPath in switchState.keyEnumerator)
{
NSNumber* boolNumber = [switchState objectForKey:indexPath];
BOOL switchOn = [boolNumber boolValue];
switch (indexPath.section) {
case basicInfoSection://basic info section (name,company,department,title)
{
int row = 0;
if(indexPath.row==row)
{
if(switchOn)
{
aContact.givenName = contact.givenName;
aContact.middleName = contact.middleName;
aContact.familyName = contact.familyName;
}
}
if(![contact.organizationName isEqualToString:@""])
{row += 1;
if(indexPath.row==row)//company row
{
//store company
if(switchOn)
aContact.organizationName = contact.organizationName;
}
}
//more code
可能有其他的解决方案来解决这个问题,以上就是我可以现在就做。
是否所有的控件都存储在一个单元中?或者,你有多个单元用于每个“CNContact”对象,其中一些可能不在屏幕上? – Stonz2
@ Stonz2对不起,我不确定你在问什么。我从CNContactViewPickerController获取CNContact对象。而对于联系人的属性(电话,电子邮件,网址..)我使用UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@“Cell”];并嵌入了开关 –
你是说你需要获取多个单元格的内容/状态,其中一些可能不在屏幕上?或者,在一个非常大的单元中你需要的是所有的东西,其中一部分可能不在屏幕上? – Stonz2