Picker View for iOS
- Add
CSPickerView.h
andCSPickerView.m
to your project; - Add
QuartzCore
framework in builds phases.
Picker works on the basis of two types of Table View, are assigned tags kCSPickerViewBackTableTag
and kCSPickerViewFrontTableTag
. These tags should use in the methods of the delegate and the data source.
kCSPickerViewBackTableTag
for the table in the back;kCSPickerViewFrontTableTag
for the table in the front.
To start, create Picker View in viewDidLoad
:
- (void)viewDidLoad
{
[super viewDidLoad];
CSPickerView *pickerView = [[CSPickerView alloc] initWithFrame:self.view.bounds];
pickerView.delegate = self;
pickerView.dataSource = self;
[self.view addSubview:pickerView];
}
You need to add a delegate (CSPickerViewDelegate
) and data source (CSPickerViewDataSource
) in your class and implement the required methods:
CSPickerViewDataSource
;- (NSInteger)pickerView:(CSPickerView *)pickerView numberOfRowsInTableView:(UITableView *)tableView
;- (UITableViewCell *)pickerView:(CSPickerView *)pickerView tableView:(UITableView *)tableView cellForRow:(NSInteger)row
;- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
(recommended);
CSPickerViewDelegate
;- (void)pickerView:(CSPickerView *)pickerView customizeTableView:(UITableView *)tableView
(optional);- (void)pickerView:(CSPickerView *)pickerView didSelectRow:(NSInteger)row
(optional).
Use the tags (kCSPickerViewBackTableTag
, kCSPickerViewFrontTableTag
) to split the definition of a table cell to the table at the back and to the table at the front:
- (UITableViewCell *)pickerView:(CSPickerView *)pickerView tableView:(UITableView *)tableView cellForRow:(NSInteger)row
{
// Create table cell.
NSString *identifier = (tableView.tag == kCSPickerViewFrontTableTag
? kCSPickerViewFrontCellIdentifier
: kCSPickerViewBackCellIdentifier);
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
if (tableView.tag == kCSPickerViewBackTableTag) {
// Customize a cell to the table in back.
} else {
// Customize a cell to the table in front.
}
}
// Populate table cell.
return cell;
}
I recommend using a different height cells:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return tableView.tag == kCSPickerViewFrontTableTag ? 60.f : 30.f;
}
A few examples implemented in the project PickerView
.
Cheapshot Picker View is available under the MIT license.