gpt4 book ai didi

ios - UITableView和UISegmentedControl

转载 作者:行者123 更新时间:2023-12-01 18:23:59 26 4
gpt4 key购买 nike

我是iPhone开发的新手。我有一个带有三个Section的UITableView,每个Section有三行。我有一个带有三个索引的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;
}
}

最佳答案

这样吧

-(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];
}

关于ios - UITableView和UISegmentedControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15240242/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com