gpt4 book ai didi

iphone - 用于下拉菜单的 UIPickerview

转载 作者:行者123 更新时间:2023-12-03 20:25:31 25 4
gpt4 key购买 nike

我有 Xcode 4.2,我正在尝试创建一个下拉菜单。我希望下拉菜单中包含文字或数字。然后,当我从列表中选择某些内容时,我希望它出现在不同页面的表格中。我的问题是:我该怎么写这个?谢谢

最佳答案

首先通过转到"file">“新建”>“新文件”并按照提示操作,创建一个带有 nib 文件的 UIViewController 子类。

Select UIViewController subclass

Name your class

现在打开 Nib (MyViewController.xib)。

MyViewController.xib

您应该将 UIPickerView 和 UITableView 拖到 View 中并按照您喜欢的方式排列它们(没关系)。

UITableView

UIPickerView

Arrange the interface elements

接下来打开 UIViewController 子类的头文件 (MyViewController.h)。

MyViewController.h

最后

@interface MyViewController : UIViewController {

行添加<UITableViewDelegate, UITableViewDataSource, UIPickerViewDelegate, UIPickerViewDataSource>所以看起来像这样

@interface MyViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, UIPickerViewDelegate, UIPickerViewDataSource>

接下来,您需要添加对表格 View 和选择器的引用以及两个值数组。就在上面@end添加以下行

@property (strong, nonatomic) IBOutlet UITableView* tableView;
@property (strong, nonatomic) IBOutlet UIPickerView* pickerView;
@property (strong, nonatomic) NSMutableArray* tableData;
@property (strong, nonatomic) NSMutableArray* pickerData;

MyViewController.h should look like this

然后返回 nib 文件并将 UITableView 和 UIPickerView 连接到这些变量。

Connect your components

现在,在源文件 (MyViewController.m) 中,您需要综合引用。所以添加@synthesize tableView, pickerView, tableData, pickerData@implementation MyViewController 下面的文件

MyViewController.m

现在要添加委托(delegate)方法,其中会有相当多的方法,但它们非常不言自明。

UITableView 的委托(delegate)方法是

- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView;

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section;

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;

UIPickerView 的委托(delegate)方法是

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;

它们应该添加到源文件中并按如下方式使用

UITableView:

- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView {
// The number of sections in the UITableView
return 1;
}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
// The number of rows in the UITableView
return [tableData count];
}

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}

// Set the table cell text to the appropriate value in tableData
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];

return cell;
}

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Whatever happens when you select a table view row.
}

UIPickerView:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
// The number of sections in the UIPickerView
return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
// The number of rows in the UIPickerView
return [pickerData count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
// The data for each row in the UIPickerView
return [pickerData objectAtIndex:row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
// whatever you want to happen when a row is selected.

// here I am assuming you want to remove from the picker and add to the table on selection
[tableData addObject:[pickerData objectAtIndex:row]];
[pickerData removeObjectAtIndex:row];

[tableView reloadData];
[pickerView reloadAllComponents];
}

MyViewController should have these methods.

好的,您要做的最后一件事是设置委托(delegate)并初始化数据。在 - (void)viewDidLoad方法MyViewController添加以下行

tableView.delegate = self;
tableView.dataSource = self;
pickerView.delegate = self;
pickerView.dataSource = self;

tableData = [[NSMutableArray alloc] init]; // table starts empty
pickerData = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3", @"4", @"5", nil]; // picker starts with values 1, 2, 3, 4, 5

[tableView reloadData];
[pickerView reloadAllComponents];

- (void)viewDidLoad should look like this.

您可以在类的规范中找到更多委托(delegate)方法,但这些方法目前应该足够了。

关于iphone - 用于下拉菜单的 UIPickerview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9342338/

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