Pau*_*tes 2 iphone xcode uipickerview xcode4.2
我有Xcode 4.2,我正在尝试创建一个下拉菜单.我希望下拉菜单下拉单词或数字.然后,当我从列表中选择某些内容时,我希望它出现在不同页面上的表格中.我的问题是:我怎么写这个?谢谢
首先通过File> New> New File并按照提示创建一个带有nib文件的UIViewController子类.


现在打开笔尖(MyViewController.xib).

您应该将UIPickerView和UITableView拖到视图中并按照您的喜好进行排列(无关紧要).



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

在结束时
@interface MyViewController : UIViewController {
Run Code Online (Sandbox Code Playgroud)
行添加<UITableViewDelegate, UITableViewDataSource, UIPickerViewDelegate, UIPickerViewDataSource>所以它看起来像这样
@interface MyViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, UIPickerViewDelegate, UIPickerViewDataSource>
Run Code Online (Sandbox Code Playgroud)
接下来,您需要添加对tableview和picker的引用以及两个值数组.在上面@end添加以下行
@property (strong, nonatomic) IBOutlet UITableView* tableView;
@property (strong, nonatomic) IBOutlet UIPickerView* pickerView;
@property (strong, nonatomic) NSMutableArray* tableData;
@property (strong, nonatomic) NSMutableArray* pickerData;
Run Code Online (Sandbox Code Playgroud)

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

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

现在要添加委托方法,它们会有很多,但它们非常自我解释.
UITableView的委托方法是
- (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;
Run Code Online (Sandbox Code Playgroud)
UIPickerView的委托方法是
- (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;
Run Code Online (Sandbox Code Playgroud)
它们应添加到源文件中并按如下方式使用
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.
}
Run Code Online (Sandbox Code Playgroud)
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];
}
Run Code Online (Sandbox Code Playgroud)

好的,您要做的最后一件事是设置委托并初始化数据.在添加以下行的- (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];
Run Code Online (Sandbox Code Playgroud)

您可以在类的规范中找到更多的委托方法,但这些方法现在应该足够了.
| 归档时间: |
|
| 查看次数: |
3476 次 |
| 最近记录: |