iOS中使用Objective-C下拉菜单
问题描述:
如何在带有箭头符号的Objective-c中下拉菜单,点击按钮时显示下拉列表。我需要这些下拉菜单两次在单个视图控制器上,看到很多链接,但是当我尝试使第二个显示任何错误或不显示列表中的数据。任何人都可以帮我吗?iOS中使用Objective-C下拉菜单
答
了解这里的概念..
我创建了U A非常简单和容易的解决方案....我希望你可以很容易地理解它。
这里我的代码:
我创建but1先but4。
然后我在图像中创建了tableview。
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 {
[email protected]"1";
table1.hidden=NO;
table2.hidden=YES;
table3.hidden=YES;
table4.hidden=YES;
[table1 reloadData];
}
- (IBAction)but2:(UIButton *)sender {
[email protected]"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 {
[email protected]"3";
table1.hidden=YES;
table2.hidden=YES;
table3.hidden=NO;
table4.hidden=YES;
[table3 reloadData];
}
- (IBAction)but4:(UIButton *)sender {
[email protected]"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
检查该第三方MKDropdownMenu - https://github.com/maxkonovalov/MKDropdownMenu – 2017-04-18 12:50:29
最好的方式来获得帮助是显示你正在做的事情和你得到的实际错误。 –
其实我还没有完成,但我需要一个两个按钮,当我们点击它时,下拉列表显示在视图控制器上。第一个下拉列表应包含列表car1,car2,car3,第二个下拉列表应显示house1,house2,house3的列表。 @PhillipMills – Hamza