使用UITableView创建下拉菜单

时间:2014-01-23 05:25:00

标签: ios objective-c

我正在尝试构建一个下拉菜单。当我单击按钮按钮时,我想要打开一个表视图,然后点击UITableViewCell。我希望单元格数据显示在按钮

.h文件

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@interface ViewController : UIViewController <UITableViewDelegate,   UITableViewDataSource>{
    IBOutlet UITableView *tblSimpleTable;
    IBOutlet UIButton *btn;
    IBOutlet UIImageView *i;
    BOOL flag;
    NSArray *arryData;
}
@property(nonatomic,retain)IBOutlet UITableView *tblSimpleTable;
@property(nonatomic,retain)IBOutlet UIButton *btn;
@property(nonatomic,retain)IBOutlet UIImageView *i;

-(IBAction)btnClicked;
@end 

的.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize btn;
@synthesize tblSimpleTable;
@synthesize i;

-(IBAction)btnClicked{
    if (flag==1) {
        flag=0;
        tblSimpleTable.hidden=NO;
        i.hidden=YES;
    }
    else{
        flag=1;
        tblSimpleTable.hidden=YES;
        i.hidden=NO;
    }
}

// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization

    }
    return self;
}

/*
 // Implement loadView to create a view hierarchy programmatically, without using a nib.
 - (void)loadView {
 }
 */



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    arryData = [[NSArray alloc] initWithObjects:@"iPhone",@"iPod",@"MacBook",@"MacBook Pro",nil];
    //tblSimpleTable.frame =CGRectMake(10, 10, 300, 100);
    flag=1;
    tblSimpleTable.hidden=YES;
    btn.layer.cornerRadius=8;
    tblSimpleTable.layer.cornerRadius=8;
    //i=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow-down.png"]];
    [super viewDidLoad];
}



/*
 // Override to allow orientations other than the default portrait orientation.
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 // Return YES for supported orientations
 return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }
 */


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}




#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [arryData 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] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] ;
    }

    // Set up the cell...
    cell.textLabel.text = [arryData objectAtIndex:indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
@end

1 个答案:

答案 0 :(得分:1)

您可以在didSelectRowAtIndexPath:

中执行此操作
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   // Changing button label
   [_btn setTitle:[arryData objectAtIndex:indexPath.row] forState:UIControlStateNormal];

   // Hiding table view
   flag=1;
   tblSimpleTable.hidden=YES;
   i.hidden=NO;
}