对的,这是可能的。您需要提取所有数据并准备一个 NSMutableArrays 并放置它。因此,您的问题放置在部分标题中,您的答案选项放置在每个部分的行中。
[在这里查看图片][1]
#import "Question.h"
@interface Question ()
{
NSMutableString *urlLString;
NSDictionary *dataDic;
NSArray *dataArray;
NSMutableArray *nmA;
NSString *question;
NSArray *optionArray;
NSMutableArray *pulledOptionArray;
NSMutableArray *pulledOptionArray1;
NSMutableArray *pulledOptionArray2;
NSMutableArray *pulledOptionArray3;
NSMutableArray *pulledOptionArray4;
NSMutableArray *pulledOptionArray5;
NSDictionary *optionDictionary;
//split
NSArray *secondsplit;
int i,j;
}
@end
@implementation Question
@synthesize checkedIndexPath;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];nmA=[[NSMutableArray alloc] init];
pulledOptionArray=[[NSMutableArray alloc] init]; pulledOptionArray1=[[NSMutableArray alloc] init];
pulledOptionArray2=[[NSMutableArray alloc] init]; pulledOptionArray3=[[NSMutableArray alloc] init];
pulledOptionArray4=[[NSMutableArray alloc] init]; pulledOptionArray5=[[NSMutableArray alloc] init];
[scroll setScrollEnabled:YES];
//[[[[[self tabBarController] viewControllers] objectAtIndex:1] tabBarItem] setEnabled:false];
inObj=[inputData globalData];
urlLString=[NSMutableString stringWithFormat:@"URl Goes Here"];
//urlLString=[urlLString1 appendString:inObj.languageTestURL];
if ([inObj.languageTestURL isEqualToString:@"C"])
{
[urlLString appendString:@"C"];
}
else if([inObj.languageTestURL isEqualToString:@"C++"])
{
[urlLString appendString:@"C++"];
}
else if([inObj.languageTestURL isEqualToString:@"java"])
{
[urlLString appendString:@"java"];
}
[page setCurrentPage:0];
NSLog(@"First");
[page setCurrentPageIndicatorTintColor:[UIColor blueColor]];
NSError *erro;
NSData *jsonData=[NSData dataWithContentsOfURL:[NSURL URLWithString:urlLString]];
NSDictionary *jLdic=[NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&erro];
NSArray *pullFromDic=[jLdic objectForKey:@"Questions"];
NSLog(@"%@",inObj.experience);
if ([inObj.experience isEqualToString:@"0-6 months[Fresher]"])
{
for (int k=0; k<=4; k++)
{
dataDic=[pullFromDic objectAtIndex:k];
question=[dataDic objectForKey:@"Question"];
optionArray=[dataDic objectForKey:@"Options"];
for (NSDictionary *nit in optionArray)
{
NSString *option=[nit objectForKey:@"option"];
// NSLog(@"%@",option);
[pulledOptionArray addObject:option];
}
[nmA addObject:question];
}
NSLog(@"%lu",(unsigned long)nmA.count);
}
else if ([inObj.experience isEqualToString:@"1-2 year[Experienced]"])
{
for (int k=5; k<=9; k++)
{
dataDic=[pullFromDic objectAtIndex:k];
NSLog(@"%@",dataDic);
}
NSLog(@"%@",dataDic);
}
else if ([inObj.experience isEqualToString:@"2+ [Well Experienced]"])
{
for (int k=10; k<=14; k++)
{
dataDic=[pullFromDic objectAtIndex:k];
NSLog(@"%@",dataDic);
}
NSLog(@"%@",dataDic);
}
// [self pullOPtions];
[self splitArray];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)page:(UIPageControl *)sender
{
if (sender.currentPage==0)
{
}
if (sender.currentPage==1) {
NSLog(@"Hello second");
}
if (sender.currentPage==2)
{
NSLog(@"Hello Third");
}
}
#pragma tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// return DataFromSource.count;
return (nmA.count-1);
// return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID=@"cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell)
{
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
}
// cell.textLabel.text=[DataFromSource objectAtIndex:indexPath.row];
if (indexPath.section==0)
{
cell.textLabel.text=[pulledOptionArray1 objectAtIndex:indexPath.row];
}
else if (indexPath.section==1)
{
cell.textLabel.text=[pulledOptionArray2 objectAtIndex:indexPath.row];
}
else if (indexPath.section==2)
{
cell.textLabel.text=[pulledOptionArray3 objectAtIndex:indexPath.row];
}
else if (indexPath.section==3)
{
cell.textLabel.text=[pulledOptionArray4 objectAtIndex:indexPath.row];
}
else if (indexPath.section==4)
{
cell.textLabel.text=[pulledOptionArray5 objectAtIndex:indexPath.row];
}
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [nmA count];
}
/*- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [nmA objectAtIndex:section];
}*/
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView* customView = [[UIView alloc] init];
UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.opaque = YES;
headerLabel.textColor = [UIColor whiteColor];
headerLabel.highlightedTextColor = [UIColor whiteColor];
headerLabel.shadowColor = [UIColor colorWithRed:0.0f/255.0f green:0.0f/255.0f blue:0.0f/255.0f alpha:0.25f];
headerLabel.shadowOffset = CGSizeMake(0.0f, -1.0f);
headerLabel.font = [UIFont boldSystemFontOfSize:16];
headerLabel.frame = CGRectMake(10.0, 0.0, 232.0,40.0);
headerLabel.numberOfLines=2;
headerLabel.text=[nmA objectAtIndex:section];
[customView setBackgroundColor:[UIColor colorWithRed:111.0f/255.0f green:168.0f/255.0f blue:220.0f/255.0f alpha:1.0]];
[customView addSubview:headerLabel];
return customView;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"%d",indexPath.section);
UITableViewCell *cell;
switch (indexPath.section)
{
case 0:
if (cell.accessoryType==UITableViewCellAccessoryCheckmark) {
cell.accessoryType=UITableViewCellAccessoryNone;
}
else{
cell.accessoryType=UITableViewCellAccessoryCheckmark;
NSLog(@"%@",[pulledOptionArray1 objectAtIndex:indexPath.row]);
}
break;
case 1:
NSLog(@"%@",[pulledOptionArray2 objectAtIndex:indexPath.row]);
break;
case 2:
NSLog(@"%@",[pulledOptionArray3 objectAtIndex:indexPath.row]);
break;
case 3:
NSLog(@"%@",[pulledOptionArray4 objectAtIndex:indexPath.row]);
break;
case 4:
NSLog(@"%@",[pulledOptionArray5 objectAtIndex:indexPath.row]);
break;
default:
break;
}
}
- (NSIndexPath*)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath*)indexPath {
for ( NSIndexPath* selectedIndexPath in taBle.indexPathsForSelectedRows ) {
if ( selectedIndexPath.section == indexPath.section )
[taBle deselectRowAtIndexPath:selectedIndexPath animated:NO] ;
}
return indexPath ;
}
/*-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
[taBle cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
}*/
#pragma table Row metrics
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 60;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 0;
}
- (UIFont *)fontForCell
{
return [UIFont boldSystemFontOfSize:18.0];
}
-(void)splitArray
{
// NSLog(@"%@",pulledOptionArray);
for (int m=0; m<4; m++)
{
NSString *nic=[pulledOptionArray objectAtIndex:m];
[pulledOptionArray1 addObject:nic];
}
for (int p=4; p<8; p++)
{
NSString *nic=[pulledOptionArray objectAtIndex:p];
[pulledOptionArray2 addObject:nic];
}
for (int p=8; p<12; p++)
{
NSString *nic=[pulledOptionArray objectAtIndex:p];
[pulledOptionArray3 addObject:nic];
}
for (int p=12; p<16; p++)
{
NSString *nic=[pulledOptionArray objectAtIndex:p];
[pulledOptionArray4 addObject:nic];
}
for (int p=16; p<20; p++)
{
NSString *nic=[pulledOptionArray objectAtIndex:p];
[pulledOptionArray5 addObject:nic];
}
}
@end
[1]: http://i.stack.imgur.com/JU0hi.png