2017-04-18 38 views
-2

如何在帶有箭頭符號的Objective-c中下拉菜單,點擊按鈕時顯示下拉列表。我需要這些下拉菜單兩次在單個視圖控制器上,看到很多鏈接,但是當我嘗試使第二個顯示任何錯誤或不顯示列表中的數據。任何人都可以幫我嗎?iOS中使用Objective-C下拉菜單

+0

檢查該第三方MKDropdownMenu - https://github.com/maxkonovalov/MKDropdownMenu – 2017-04-18 12:50:29

+0

最好的方式來獲得幫助是顯示你正在做的事情和你得到的實際錯誤。 –

+0

其實我還沒有完成,但我需要一個兩個按鈕,當我們點擊它時,下拉列表顯示在視圖控制器上。第一個下拉列表應包含列表car1,car2,car3,第二個下拉列表應顯示house1,house2,house3的列表。 @PhillipMills – Hamza

回答

0

瞭解這裏的概念..

我創建了U A非常簡單和容易的解決方案....我希望你可以很容易地理解它。

這裏我的代碼:

故事板: enter image description here

我創建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 
+0

它顯示錯誤在didSelectionRowAtIndexPath委託方法中顯示「but1」和「but2」屬性nit在類型爲'XXTableViewController類'的對象上找到的屬性。在你的代碼底部。 @NAVEEN KUMAR – Hamza

+0

這是工作完美的,但一個問題來了,我想要一個按鈕取消下拉列表按鈕。當我點擊下拉按鈕列表時,應該出現在它下方的按鈕下方,並在下拉列表中滾動項目@NAVEEN KUMAR – Hamza

+0

我有共享的截圖,請檢查出來@NAVEEN KUMAR – Hamza