我是iPhone開發的新手。我有一個UITableView有三個部分,每個部分有三個行。我有一個UISegmentedControl有三個索引。tableview最初是隱藏的。當我選擇segmentIndex的任何索引時,它將顯示帶有三個部分的tableview。 但我的問題是,當我選擇分段控制則其顯示的tableView的指標只有一個部分和另外兩個部分是隱藏在tableView.How做到這一點請回答
這裏是我的代碼UITableView和UISegmentedControl
#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;
}
}
重裝在每段點擊 – Rushabh 2013-03-06 06:17:12
reloed表的表中的每一個部分都顯示三段在我的tableview。但是我只想顯示一個部分,另外兩個部分是隱藏的。 – 2013-03-06 06:23:58