IOS 中的下拉列表使用 Objective-C
DropDown in IOS using Objective-C
如何在 Objective-c 中使用带箭头符号的下拉菜单,单击按钮时会显示下拉列表。我需要在单个视图控制器上两次使用这些下拉菜单,已经看到很多链接,但是当我尝试创建第二个链接时,它显示任何错误或不在列表中显示数据。谁能帮帮我?????
理解这里的概念..
我为你创建了一个非常简单易行的解决方案....我希望你能轻松理解它。
这里是我的代码:
故事板:
我先创建了 but1 到 but4。
然后我在图像中创建了表格视图。
ViewController.h
//button Actions
- (IBAction)but1:(UIButton *)sender;
- (IBAction)but2:(UIButton *)sender;
- (IBAction)but3:(UIButton *)sender;
- (IBAction)but4:(UIButton *)sender;
//table properties
@property (weak, nonatomic) IBOutlet UITableView *table1;
@property (weak, nonatomic) IBOutlet UITableView *table2;
@property (weak, nonatomic) IBOutlet UITableView *table3;
@property (weak, nonatomic) IBOutlet UITableView *table4;
//button properties
@property (weak, nonatomic) IBOutlet UIButton *but1;
@property (weak, nonatomic) IBOutlet UIButton *but2;
@property (weak, nonatomic) IBOutlet UIButton *but3;
@property (weak, nonatomic) IBOutlet UIButton *but4;
ViewController.m
#import "ViewController.h"
@interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate,UITableViewDelegate,UITableViewDataSource>{
// UIImagePickerController *picker;
NSString *ImageViewC;
NSArray *data;
NSArray *data1;
}
@end
@implementation ViewController
@synthesize table1,table2,table3,table4;
- (void)viewDidLoad {
[super viewDidLoad];
data=[[NSArray alloc]initWithObjects:@"car1",@"car2",@"car1",@"car2", nil];
data1=[[NSArray alloc]initWithObjects:@"house1",@"house2",@"house3",@"house4", nil];
table2.dataSource=self;
table1.dataSource=self;
table3.dataSource=self;
table4.dataSource=self;
table2.delegate=self;
table1.delegate=self;
table3.delegate=self;
table4.delegate=self;
table1.hidden=YES;
table2.hidden=YES;
table3.hidden=YES;
table4.hidden=YES;
// picker = [[UIImagePickerController alloc]init];
// picker.delegate=self;
//
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)but1:(UIButton *)sender {
ImageViewC=@"1";
table1.hidden=NO;
table2.hidden=YES;
table3.hidden=YES;
table4.hidden=YES;
[table1 reloadData];
}
- (IBAction)but2:(UIButton *)sender {
ImageViewC=@"2";
table1.hidden=YES;
table2.hidden=NO;
table3.hidden=YES;
table4.hidden=YES;
[table2 reloadData];
// [picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
// [self presentViewController:picker animated:YES completion:NULL];
}
- (IBAction)but3:(UIButton *)sender {
ImageViewC=@"3";
table1.hidden=YES;
table2.hidden=YES;
table3.hidden=NO;
table4.hidden=YES;
[table3 reloadData];
}
- (IBAction)but4:(UIButton *)sender {
ImageViewC=@"4";
table1.hidden=YES;
table2.hidden=YES;
table3.hidden=YES;
table4.hidden=NO;
[table4 reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if ([ImageViewC isEqualToString:@"1"])
return [data count];
else
return [data1 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];
}
if ([ImageViewC isEqualToString:@"1"]) {
cell.textLabel.text = [data objectAtIndex:indexPath.row];
}else if([ImageViewC isEqualToString:@"2"]){
cell.textLabel.text = [data1 objectAtIndex:indexPath.row];
}else if ([ImageViewC isEqualToString:@"3"]){
cell.textLabel.text=[data objectAtIndex:indexPath.row];
}else{
cell.textLabel.text=[data1 objectAtIndex:indexPath.row];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *selectedCell=[tableView cellForRowAtIndexPath:indexPath];
NSLog(@"%@",selectedCell.textLabel.text);
if ([ImageViewC isEqualToString:@"1"]) {
[self.but1 setTitle:[NSString stringWithFormat:@"%@",selectedCell.textLabel.text] forState:UIControlStateNormal];
}else if ([ImageViewC isEqualToString:@"2"]){
[self.but2 setTitle:[NSString stringWithFormat:@"%@",selectedCell.textLabel.text] forState:UIControlStateNormal];
}else if ([ImageViewC isEqualToString:@"3"]){
[self.but3 setTitle:[NSString stringWithFormat:@"%@",selectedCell.textLabel.text] forState:UIControlStateNormal];
}else{
[self.but4 setTitle:[NSString stringWithFormat:@"%@",selectedCell.textLabel.text] forState:UIControlStateNormal];
}
table1.hidden=YES;
table2.hidden=YES;
table3.hidden=YES;
table4.hidden=YES;
}
@end
如何在 Objective-c 中使用带箭头符号的下拉菜单,单击按钮时会显示下拉列表。我需要在单个视图控制器上两次使用这些下拉菜单,已经看到很多链接,但是当我尝试创建第二个链接时,它显示任何错误或不在列表中显示数据。谁能帮帮我?????
理解这里的概念..
我为你创建了一个非常简单易行的解决方案....我希望你能轻松理解它。
这里是我的代码:
故事板:
我先创建了 but1 到 but4。
然后我在图像中创建了表格视图。
ViewController.h
//button Actions
- (IBAction)but1:(UIButton *)sender;
- (IBAction)but2:(UIButton *)sender;
- (IBAction)but3:(UIButton *)sender;
- (IBAction)but4:(UIButton *)sender;
//table properties
@property (weak, nonatomic) IBOutlet UITableView *table1;
@property (weak, nonatomic) IBOutlet UITableView *table2;
@property (weak, nonatomic) IBOutlet UITableView *table3;
@property (weak, nonatomic) IBOutlet UITableView *table4;
//button properties
@property (weak, nonatomic) IBOutlet UIButton *but1;
@property (weak, nonatomic) IBOutlet UIButton *but2;
@property (weak, nonatomic) IBOutlet UIButton *but3;
@property (weak, nonatomic) IBOutlet UIButton *but4;
ViewController.m
#import "ViewController.h"
@interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate,UITableViewDelegate,UITableViewDataSource>{
// UIImagePickerController *picker;
NSString *ImageViewC;
NSArray *data;
NSArray *data1;
}
@end
@implementation ViewController
@synthesize table1,table2,table3,table4;
- (void)viewDidLoad {
[super viewDidLoad];
data=[[NSArray alloc]initWithObjects:@"car1",@"car2",@"car1",@"car2", nil];
data1=[[NSArray alloc]initWithObjects:@"house1",@"house2",@"house3",@"house4", nil];
table2.dataSource=self;
table1.dataSource=self;
table3.dataSource=self;
table4.dataSource=self;
table2.delegate=self;
table1.delegate=self;
table3.delegate=self;
table4.delegate=self;
table1.hidden=YES;
table2.hidden=YES;
table3.hidden=YES;
table4.hidden=YES;
// picker = [[UIImagePickerController alloc]init];
// picker.delegate=self;
//
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)but1:(UIButton *)sender {
ImageViewC=@"1";
table1.hidden=NO;
table2.hidden=YES;
table3.hidden=YES;
table4.hidden=YES;
[table1 reloadData];
}
- (IBAction)but2:(UIButton *)sender {
ImageViewC=@"2";
table1.hidden=YES;
table2.hidden=NO;
table3.hidden=YES;
table4.hidden=YES;
[table2 reloadData];
// [picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
// [self presentViewController:picker animated:YES completion:NULL];
}
- (IBAction)but3:(UIButton *)sender {
ImageViewC=@"3";
table1.hidden=YES;
table2.hidden=YES;
table3.hidden=NO;
table4.hidden=YES;
[table3 reloadData];
}
- (IBAction)but4:(UIButton *)sender {
ImageViewC=@"4";
table1.hidden=YES;
table2.hidden=YES;
table3.hidden=YES;
table4.hidden=NO;
[table4 reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if ([ImageViewC isEqualToString:@"1"])
return [data count];
else
return [data1 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];
}
if ([ImageViewC isEqualToString:@"1"]) {
cell.textLabel.text = [data objectAtIndex:indexPath.row];
}else if([ImageViewC isEqualToString:@"2"]){
cell.textLabel.text = [data1 objectAtIndex:indexPath.row];
}else if ([ImageViewC isEqualToString:@"3"]){
cell.textLabel.text=[data objectAtIndex:indexPath.row];
}else{
cell.textLabel.text=[data1 objectAtIndex:indexPath.row];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *selectedCell=[tableView cellForRowAtIndexPath:indexPath];
NSLog(@"%@",selectedCell.textLabel.text);
if ([ImageViewC isEqualToString:@"1"]) {
[self.but1 setTitle:[NSString stringWithFormat:@"%@",selectedCell.textLabel.text] forState:UIControlStateNormal];
}else if ([ImageViewC isEqualToString:@"2"]){
[self.but2 setTitle:[NSString stringWithFormat:@"%@",selectedCell.textLabel.text] forState:UIControlStateNormal];
}else if ([ImageViewC isEqualToString:@"3"]){
[self.but3 setTitle:[NSString stringWithFormat:@"%@",selectedCell.textLabel.text] forState:UIControlStateNormal];
}else{
[self.but4 setTitle:[NSString stringWithFormat:@"%@",selectedCell.textLabel.text] forState:UIControlStateNormal];
}
table1.hidden=YES;
table2.hidden=YES;
table3.hidden=YES;
table4.hidden=YES;
}
@end