Ios 在UIPickerView中的单个组件中附加两个组件
从昨天起我就有一个很小的问题。数据库中有两个表,一个是项目表,另一个是福利表。我想在单个PickerView组件中添加两个表数据。例如,项目有pro1、proj2、proj3,收益表有benefits1、benefits2。因此,我想在单个PickerView组件中附加项目表dat和福利表数据Ios 在UIPickerView中的单个组件中附加两个组件,ios,append,uipickerview,Ios,Append,Uipickerview,从昨天起我就有一个很小的问题。数据库中有两个表,一个是项目表,另一个是福利表。我想在单个PickerView组件中添加两个表数据。例如,项目有pro1、proj2、proj3,收益表有benefits1、benefits2。因此,我想在单个PickerView组件中附加项目表dat和福利表数据 -(void)loadprojects { NSString *post =[[NSString alloc] initWithFormat:@"username=%@",[se
-(void)loadprojects
{
NSString *post =[[NSString alloc] initWithFormat:@"username=%@",[self.projectpicker dataSource]];
// Code for Project loading
NSString * BenefitString =@"http://test.com/GetBenefitTypes";
NSURL *Benefiturl = [NSURL URLWithString:BenefitString];
NSString *projecturltemp = @"http://test.com/GetAssignedProjects";
NSString *str = [[NSUserDefaults standardUserDefaults] valueForKey:@"UserLoginIdSession"];
NSString *usrid = str;
NSString * projecturl =[NSString stringWithFormat:@"%@/%@",projecturltemp,usrid];
NSURL *url = [NSURL URLWithString:projecturl];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:Benefiturl];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/projectpicker" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/jsonArray" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSError *error = [[NSError alloc] init];
NSHTTPURLResponse *response = nil;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestReturnCacheDataElseLoad
timeoutInterval:30];
NSURLRequest *urlRequestBenifits = [NSURLRequest requestWithURL:Benefiturl
cachePolicy:NSURLRequestReturnCacheDataElseLoad
timeoutInterval:30];
// Make synchronous request
urlData = [NSURLConnection sendSynchronousRequest:urlRequest
returningResponse:&response
error:&error];
urlData = [NSURLConnection sendSynchronousRequest:urlRequestBenifits
returningResponse:&response
error:&error];
if ([response statusCode] >= 200 && [response statusCode] < 300)
{
NSString *responseData = [NSJSONSerialization JSONObjectWithData:urlData
options:NSJSONReadingAllowFragments error:&error];
NSArray *entries = [NSJSONSerialization JSONObjectWithData:[responseData dataUsingEncoding:NSUTF8StringEncoding]
options:0 error:&error];
if(!entries)
{
NSLog(@"Error : %@", error);
}
else{
for (NSDictionary *entry in entries) {
projID = [entries valueForKey:@"ID_PROJECT"];
projectNames = [entries valueForKey:@"NM_PROJECT"];
BenefitsNames = [entries valueForKey:@"NM_LEAVES"];
}
//Combined = [BenefitsNames arrayByAddingObjectsFromArray:projectNames];
NSLog(@"Combined : %@", projectNames);
//NSLog(@"projID : %@", projID);
_projectpicker.delegate = self;
_projectpicker.dataSource = self;
}
} else {
}
}
-(无效)加载项目
{
NSString*post=[[NSString alloc]initWithFormat:@“username=%@,[self.projectpicker数据源]];
//工程装载规范
NSString*BenefitString=@”http://test.com/GetBenefitTypes";
NSURL*Benefiturl=[NSURL URLWithString:BenefitString];
NSString*projecturltemp=@”http://test.com/GetAssignedProjects";
NSString*str=[[NSUserDefaults standardUserDefaults]valueForKey:@“UserLoginIdSession”];
NSString*usrid=str;
NSString*projecturl=[NSString stringWithFormat:@“%@/%@”,projecturltemp,usrid];
NSURL*url=[NSURL URLWithString:projecturl];
NSData*postData=[post数据使用编码:NSASCIIStringEncoding allowLossyConversion:是];
NSString*postLength=[NSString stringWithFormat:@“%lu”,(无符号长)[postData长度]];
NSMutableURLRequest*请求=[[NSMutableURLRequest alloc]init];
[请求设置URL:Benefiturl];
[请求设置url:url];
[请求设置HttpMethod:@“POST”];
[请求设置值:HttpHeaderField的postLength:@“内容长度”];
[请求设置值:@“应用程序/项目选择器”用于HttpHeaderField:@“接受”];
[请求设置值:@“应用程序/jsonArray”用于HttpHeaderField:@“内容类型”];
[请求setHTTPBody:postData];
NSError*error=[[NSError alloc]init];
NSHTTPURLResponse*响应=nil;
NSData*urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&响应错误:&错误];
NSURLRequest*urlRequest=[nsurlRequestRequestWithURL:url
cachePolicy:NSURLRequestReturnCacheDataElseLoad
时间间隔:30];
NSURLRequest*urlRequestBenifits=[NsurlRequestRequestWithURL:Benefiturl
cachePolicy:NSURLRequestReturnCacheDataElseLoad
时间间隔:30];
//发出同步请求
urlData=[NSURLConnection sendSynchronousRequest:urlRequest
returningResponse:&响应
错误:&错误];
urlData=[NSURLConnection sendSynchronousRequest:urlRequestBenifits
returningResponse:&响应
错误:&错误];
如果([response statusCode]>=200&&[response statusCode]<300)
{
NSString*responseData=[NSJSONSerialization JSONObjectWithData:urlData
选项:NSJSONReadingAllowFragments错误:&错误];
NSArray*entries=[NSJSONSerialization JSONObjectWithData:[responseData dataUsingEncoding:NSUTF8StringEncoding]
选项:0错误:&错误];
如果(!条目)
{
NSLog(@“错误:%@”,错误);
}
否则{
for(NSDictionary*条目中的条目){
projID=[entries valueForKey:@“ID_PROJECT”];
项目名称=[entries valueForKey:@“NM_项目”];
收益表=[entries valueForKey:@“NM_LEAVES”];
}
//合并=[BenefitsNames ArrayByAddingObject Romarray:projectNames];
NSLog(@“合并:%@”,项目名称);
//NSLog(@“项目:%”,项目);
_projectpicker.delegate=self;
_projectpicker.dataSource=self;
}
}否则{
}
}
项目和好处是否是NSArray实例?如果是这样的话,为什么不把它们合并成一个数组,然后继续使用呢
NSArray *projects = @[@"pro 1", @"pro 2"];
NSArray *benefits = @[@"benefit 1", @"benefit 2"];
NSArray *combined = [projects arrayByAddingObjectsFromArray:benefits];
NSLog(@"Combined: %@", combined);
组合打印输出:
Combined: (
"pro 1",
"pro 2",
"benefit 1",
"benefit 2" )
然后在-(NSString*)pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)row for component:(NSInteger)component
方法中,只需返回[combined objectAtIndex:row]代码>
根据我的理解,你想要像上面那样的东西吗?我说得对吗?那么UIPickerView中有多个组件
如果是这样的话,首先,您需要设置ViewController的委派->
<UIPickerViewDataSource, UIPickerViewDelegate>
不清楚您所说的“将两个组件合并为一个”是什么意思
但我猜你们想要项目和效益的结合,就像项目:效益
如果是这样,您可以使用以下代码段
NSArray *arrProject=[NSArray arrayWithObjects:@"Project1",@"Project2",nil];
NSArray *arrBenefits=[NSArray arrayWithObjects:@"Benefits1",@"Benefits2",nil];
NSMutableArray *arrCombined=[[NSMutableArray alloc]init];
for(int i=0;i<[arrProject count];i++)
{
[arrCombined addObject:[NSString stringWithFormat:@"%@:%@",[arrProject objectAtIndex:i],[arrBenefits objectAtIndex:i]]];
}
NSLog(@"Combined:%@",arrCombined);
NSArray*arrProject=[NSArray阵列及其对象:@“Project1”,“Project2”,nil];
NSArray*arrBenefits=[NSArray阵列,其对象为:@“Benefits1”,@“Benefits2”,无];
NSMutableArray*arrCombined=[[NSMutableArray alloc]init];
对于(int i=0;i为什么您不使用2个组件?到目前为止您尝试了什么?什么不起作用?我的pickerview中有三个组件,我想将其添加到1个组件中的项目中。我可以加载所有组件中的所有数据,但我想将此好处添加到组件1中的项目组件中,作为1个长列表?或者您想将字符串名称附加在一起显示吗?你能显示项目名称和项目名称的结果吗?我想我们很接近,但我不知道哪里出了问题,让我为你编辑代码。没有Seph你弄错了我,假设我在组件1中有福利数据,在组件2中有项目数据,我想要这两个福利和将数据投影到一个组件中
NSArray *arrProject=[NSArray arrayWithObjects:@"Project1",@"Project2",nil];
NSArray *arrBenefits=[NSArray arrayWithObjects:@"Benefits1",@"Benefits2",nil];
NSMutableArray *arrCombined=[[NSMutableArray alloc]init];
for(int i=0;i<[arrProject count];i++)
{
[arrCombined addObject:[NSString stringWithFormat:@"%@:%@",[arrProject objectAtIndex:i],[arrBenefits objectAtIndex:i]]];
}
NSLog(@"Combined:%@",arrCombined);