Iphone 使用一个TableViewController类创建两个tableview列表

Iphone 使用一个TableViewController类创建两个tableview列表,iphone,objective-c,xcode,uitableview,Iphone,Objective C,Xcode,Uitableview,我想做一个示例应用程序,在视图中我有两个按钮,一个是国家,另一个是州。 当我点击国家按钮时,国家列表应该像弹出窗口一样出现在tableview类中,当我点击国家按钮时,国家列表应该像弹出窗口一样出现在tableview类中,因此我如何做到这一点请用示例代码提出建议 注意:对于国家/地区和州数据列表,我应该只使用一个TableViewcontroller类。问题还不够清楚,您尝试了什么。。 但是,您可以使用UIPopOverController来实现这一点 看见 或者只是同一Nib文件中的静态U

我想做一个示例应用程序,在视图中我有两个按钮,一个是国家,另一个是州。 当我点击国家按钮时,国家列表应该像弹出窗口一样出现在tableview类中,当我点击国家按钮时,国家列表应该像弹出窗口一样出现在tableview类中,因此我如何做到这一点请用示例代码提出建议


注意:对于国家/地区和州数据列表,我应该只使用一个TableViewcontroller类。

问题还不够清楚,您尝试了什么。。 但是,您可以使用UIPopOverController来实现这一点 看见


或者只是同一Nib文件中的静态UiTableView,在不需要时将其隐藏。

问题还不够清楚,您尝试了什么。。 但是,您可以使用UIPopOverController来实现这一点 看见


或者只是同一Nib文件中的静态UiTableView,在不需要时将其隐藏。

使用两个表视图和两个按钮

将每个tableview放置在每个按钮下方


最初,两个表视图都是隐藏的。单击按钮时,显示带有动画的TableView。

使用两个TableView和两个按钮

将每个tableview放置在每个按钮下方


最初,两个表视图都是隐藏的。单击按钮时,显示带有动画的TableView。

我认为您应该只使用一个TableViewController,但具有不同的数据源。

我认为您应该只使用一个TableViewController,但具有不同的数据源。

您可以使用一个TableView进行此操作:这里我仅附加逻辑

在viewdidload中

将有两个数组countryArray和stateArray

将有第三个数组:tempArray

有两个按钮:按钮1和按钮2 tableview.hidden=YES

在button1Action中,将countryArray分配给tempArray,并[tableview重新加载]

在button2Action中,将stateArray分配给tempArray,并将[tableview重新加载]

然后在tableview中

  • (NSInteger)表格视图:(UITableView*)表格行数节:(NSInteger)节 {

    返回[tempArray count]

}

然后在

  • (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
使用临时数组

}


试试这个…如果你想要更详细的信息,请通知…

你可以使用一个Tableview:这里我只附上逻辑

在viewdidload中

将有两个数组countryArray和stateArray

将有第三个数组:tempArray

有两个按钮:按钮1和按钮2 tableview.hidden=YES

在button1Action中,将countryArray分配给tempArray,并[tableview重新加载]

在button2Action中,将stateArray分配给tempArray,并将[tableview重新加载]

然后在tableview中

  • (NSInteger)表格视图:(UITableView*)表格行数节:(NSInteger)节 {

    返回[tempArray count]

}

然后在

  • (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
使用临时数组

}

试试这个……如果您想要更详细的信息,请告知……

以下是代码:

RootViewController.h

@interface RootViewController : UIViewController {

UIButton *btnCountry;
UIButton *btnState;

NSMutableArray *tempArray;
NSMutableArray *countryArray;
NSMutableArray *stateArray;

IBOutlet UITableView *tempTable;
}

 @property (nonatomic,retain) UIButton *btnCountry;
 @property (nonatomic,retain) UIButton *btnState;
 @property (nonatomic,retain) NSMutableArray *countryArray;
 @property (nonatomic,retain) NSMutableArray *stateArray;
@property (nonatomic,retain) NSMutableArray *tempArray;
@property (nonatomic,retain) UITableView *tempTable;


- (IBAction) showState:(id)sender;
- (IBAction) showCountry:(id)sender;
@结束

RootViewController.m

@implementation RootViewController

@synthesize btnState,btnCountry, stateArray,countryArray,tempArray;
@synthesize tempTable;

#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {

[super viewDidLoad];

tempTable.hidden = YES;

countryArray = [[NSMutableArray alloc]initWithObjects:@"India",@"Pakistan",@"USA",nil];
stateArray = [[NSMutableArray alloc]initWithObjects:@"Gujarat",@"Maharashtra", @"Karnataka",nil];
tempArray = [[NSMutableArray alloc]init];

}



- (IBAction) showCountry:(id)sender
{
btnCountry = (UIButton *)sender;
tempArray = countryArray;
[tempTable reloadData];
if([btnCountry isSelected])
{
    tempTable.hidden = YES;
    btnCountry.selected = NO;
}
else
{
    tempTable.hidden = NO;
    btnCountry.selected = YES;
}
}


- (IBAction) showState:(id)sender
{
btnState = (UIButton *)sender;
tempArray = stateArray;
[tempTable reloadData];

if([btnState isSelected])
{
    tempTable.hidden = YES;
    btnState.selected = NO;
}
else
{
    tempTable.hidden = NO;
    btnState.selected = YES;
}
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section      {

return [tempArray count];
 }


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

cell.textLabel.text = [tempArray objectAtIndex:indexPath.row];

return cell;
}


@end
代码如下:

RootViewController.h

@interface RootViewController : UIViewController {

UIButton *btnCountry;
UIButton *btnState;

NSMutableArray *tempArray;
NSMutableArray *countryArray;
NSMutableArray *stateArray;

IBOutlet UITableView *tempTable;
}

 @property (nonatomic,retain) UIButton *btnCountry;
 @property (nonatomic,retain) UIButton *btnState;
 @property (nonatomic,retain) NSMutableArray *countryArray;
 @property (nonatomic,retain) NSMutableArray *stateArray;
@property (nonatomic,retain) NSMutableArray *tempArray;
@property (nonatomic,retain) UITableView *tempTable;


- (IBAction) showState:(id)sender;
- (IBAction) showCountry:(id)sender;
@结束

RootViewController.m

@implementation RootViewController

@synthesize btnState,btnCountry, stateArray,countryArray,tempArray;
@synthesize tempTable;

#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {

[super viewDidLoad];

tempTable.hidden = YES;

countryArray = [[NSMutableArray alloc]initWithObjects:@"India",@"Pakistan",@"USA",nil];
stateArray = [[NSMutableArray alloc]initWithObjects:@"Gujarat",@"Maharashtra", @"Karnataka",nil];
tempArray = [[NSMutableArray alloc]init];

}



- (IBAction) showCountry:(id)sender
{
btnCountry = (UIButton *)sender;
tempArray = countryArray;
[tempTable reloadData];
if([btnCountry isSelected])
{
    tempTable.hidden = YES;
    btnCountry.selected = NO;
}
else
{
    tempTable.hidden = NO;
    btnCountry.selected = YES;
}
}


- (IBAction) showState:(id)sender
{
btnState = (UIButton *)sender;
tempArray = stateArray;
[tempTable reloadData];

if([btnState isSelected])
{
    tempTable.hidden = YES;
    btnState.selected = NO;
}
else
{
    tempTable.hidden = NO;
    btnState.selected = YES;
}
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section      {

return [tempArray count];
 }


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

cell.textLabel.text = [tempArray objectAtIndex:indexPath.row];

return cell;
}


@end

不鼓励弹出窗口。最好的方法是使用带有两个部分的pickerview,一个包含所有国家,另一个包含州。不鼓励弹出窗口。最好的方法是使用一个包含两个部分的pickerview,一个包含所有国家,另一个包含国家。是的,我遵循的是相同的链接。在上面的链接中,当我们单击按钮列表时,会出现。同样的方法,我希望在按钮旁边再添加一个按钮,例如“国家”单击“国家/地区”时,列表应使用相同的tableViewController类显示。如何执行此操作?列表中的数据应不同。Lsn,创建另一个具有两个方法的类,一个用于国家/地区,另一个用于状态或其他任何方法。每个函数返回NSMutableArray,,以便您可以在Uitableviewcontroller类中调用它们,,在主nib文件中,当您调用uitableview类时,请确保传递一个参数,直到uitableview调用要加载到表中的函数为止,这将需要很长时间才能完成,但在添加更多表时会节省一些时间!!我希望你明白了!!是的,我正在跟踪相同的链接。在上面的链接中,当我们单击按钮列表时,会显示一个按钮。同样,我希望在按钮旁边再添加一个按钮,例如,当我单击国家列表时,应使用相同的tableViewController类显示“国家”。我们如何做到这一点?列表中的数据应不同。Lsn,使另一个类具有两个方法,一个用于国家,另一个用于州或其他任何对象。每个函数返回一个NSMutableArray,,以便可以在Uitableviewcontroller类中调用它们,,在主nib文件中,当您调用uitableview类时,请确保传递一个参数,直到uitableview调用要加载到表中的函数为止,这将需要很长时间才能完成,但在添加更多表时会节省一些时间!!我希望你明白了!!不,实际上我只需要使用一个tableView,因为如果需要更多的按钮列表,我无法继续添加更多的tableView,这就是原因。然后使用两个临时数组(国家/地区和州)并保存您希望在此数组中显示的任何数据。现在,当选择“国家/地区”按钮时,将临时数组(国家/地区)的值分配给TableView.viceversa由于我是ios新手,我如何才能这样做?我可以输入一些示例代码吗?事实上,我只需要使用一个TableView,因为如果需要输入更多按钮列表,我无法继续添加更多的TableView,这就是原因。然后使用两个临时数组(国家和州)并保存您想要显示的任何数据