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



我想做一个示例应用程序,在一个视图中我有两个按钮,一个是国家,另一个是国家。当我点击一个国家按钮的国家列表应该出现在一个tableview类像弹出窗口,当我点击状态按钮状态列表应该出现在一个tableview像弹出窗口,所以我怎么能做到这一点,请建议与示例代码。

注意:我应该只使用一个TableViewcontroller类为国家和国家的数据列表。

代码:

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;

@end

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

这个问题不够清楚,你尝试了什么?..你可以用UIPopOverController来做那个看到这个链接

或者只是在同一个Nib文件中的一个静态UiTableView,当你不需要它的时候把它隐藏起来

你可以使用一个表视图:这里我只附加逻辑。

在viewdidload

将有两个数组countryArray和stateArray。

将会有第三个数组:tempArray

有两个按钮:button1和button2tableview.hidden =是的,

在button1Action中将countryArray赋值给tempArray和[tableview reload]

在button2Action中将stateArray赋值给tempArray和[tableview reload]

then in tableview delegate,

  • (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section{

    return [tempArray count];

}

,然后在

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

使用tempArray……

}

试试这个…如果您需要更详细的信息,请通知....

取两个tableview和两个button。

将每个tableview放置在每个按钮的下面。

最初两个tableview都是隐藏的。当按钮被点击时,用动画显示TableView

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

最新更新