我是 iPhone 开发的新手。我有一个 UITableView 三个部分,每个部分有三行。我有一个带有三个索引的 UISegmentedControl。tableview 最初是隐藏的。当我选择 segmentIndex 的任何索引时,它会显示三个部分的 tableview。但我的问题是,当我选择分段控件的索引时,它会显示只有一个部分的 tableView,而另外两个部分隐藏在 tableView 中。如何做到这一点请回答
这是我的代码
#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;
}
}