gpt4 book ai didi

ios - 使用 UITableView 创建下拉菜单

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:19:18 24 4
gpt4 key购买 nike

我正在尝试构建一个下拉菜单。当我单击一个按钮按钮时,我希望打开一个 TableView ,然后点击 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

最佳答案

您可以在 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;
}

关于ios - 使用 UITableView 创建下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21300053/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com