I am new in iPhone developing. I have one UITableView with three Section and each Section has three rows. And I have one UISegmentedControl with three index.the tableview is hidden initially .when I select any index of segmentIndex then its display the tableview with three section.
But my question is that when I select index of segmented control then its display the tableView with only one section and other two section is hide in the tableView.How to do it please answer
Here is my code
#import "DetailViewController.h"
@interface DetailViewController ()
@end
@implementation DetailViewController
@synthesize tblView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
//Customize tableview
tblView = [[UITableView alloc] init];
firstName = [NSArray arrayWithObjects:@"Parth",@"Bhadresh",@"Marshal", nil];
middleName = [NSArray arrayWithObjects:@"Dipakbhai",@"Dineshbhai",@"Mansukhbhai",nil];
lastName = [NSArray arrayWithObjects:@"Patel",@"Laiya",@"Kaneria", nil];
tblView.delegate = self;
tblView.dataSource = self;
}
-(void)viewWillAppear:(BOOL)animated
{
sgmtControl.frame = CGRectMake(17, 180, 285, 44);
tblView.frame = CGRectMake(0, 0, 320, 364);
tblView.hidden = YES;
[self.view addSubview:tblView];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
switch (section)
{
case 0:
return 3;
break;
case 1:
return 3;
break;
case 2:
return 3;
break;
}
return 0;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection: (NSInteger)section
{
switch (section)
{
case 0:
return @"FirstName";
break;
case 1:
return @"MiddleName";
break;
case 2:
return @"LastName";
break;
}
return nil;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *myIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myIdentifier];
}
switch (indexPath.section)
{
case 0:
cell.textLabel.text =[firstName objectAtIndex:indexPath.row];
break;
case 1:
cell.textLabel.text = [middleName objectAtIndex:indexPath.row];
break;
case 2:
cell.textLabel.text = [lastName objectAtIndex:indexPath.row];
break;
}
return cell;
}
-(IBAction)changeSegement:(id)sender
{
if(sgmtControl.selectedSegmentIndex == 0)
{
tblView.hidden = NO;
}
else if (sgmtControl.selectedSegmentIndex == 1)
{
tblView.hidden = NO;
}
else if (sgmtControl.selectedSegmentIndex == 2)
{
tblView.hidden = NO;
}
}
Do like this,
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
switch (sgmtControl.selectedSegmentIndex)
{
case 0:
return [firstName count];
break;
case 1:
return [middleName count];
break;
case 2:
return [lastName count];
break;
}
return 0;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection: (NSInteger)section
{
switch (sgmtControl.selectedSegmentIndex)
{
case 0:
return @"FirstName";
break;
case 1:
return @"MiddleName";
break;
case 2:
return @"LastName";
break;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *myIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myIdentifier];
}
switch (sgmtControl.selectedSegmentIndex)
{
case 0:
cell.textLabel.text =[firstName objectAtIndex:indexPath.row];
break;
case 1:
cell.textLabel.text = [middleName objectAtIndex:indexPath.row];
break;
case 2:
cell.textLabel.text = [lastName objectAtIndex:indexPath.row];
break;
}
return cell;
}
-(IBAction)changeSegement:(id)sender
{
if(sgmtControl.selectedSegmentIndex == 0)
{
tblView.hidden = NO;
}
else if (sgmtControl.selectedSegmentIndex == 1)
{
tblView.hidden = NO;
}
else if (sgmtControl.selectedSegmentIndex == 2)
{
tblView.hidden = NO;
}
[tblView reloadData];
}