Сначала создайте подкласс UIViewController с nib-файлом, выбрав «Файл»> «Создать»> «Новый файл» и следуя подсказкам.


Теперь откройте перо (MyViewController.xib).

Вы должны перетащить UIPickerView и UITableView в представление и расположить их так, как вам нравится (это не имеет значения).



Затем откройте файл заголовка (MyViewController.h) для вашего подкласса UIViewController.

В конце
@interface MyViewController : UIViewController {
line add <UITableViewDelegate, UITableViewDataSource, UIPickerViewDelegate, UIPickerViewDataSource>
, чтобы он выглядел следующим образом
@interface MyViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, UIPickerViewDelegate, UIPickerViewDataSource>
Далее вам нужно будет добавить ссылки на ваш просмотр таблицы и средство выбора, а также два массива значений. Чуть выше @end
добавить следующие строки
@property (strong, nonatomic) IBOutlet UITableView* tableView;
@property (strong, nonatomic) IBOutlet UIPickerView* pickerView;
@property (strong, nonatomic) NSMutableArray* tableData;
@property (strong, nonatomic) NSMutableArray* pickerData;

Затем просто вернитесь к файлу 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;
Методы делегата для 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;
Они должны быть добавлены в исходный файл и использоваться следующим образом
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];
}

Хорошо, последнее, что вам нужно сделать, это установить делегатов и инициализировать данные. В методе - (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];

Есть еще несколько методов делегатов, которые вы можете найти в спецификации для классов, но на данный момент их должно быть достаточно.