I have a normal UITableView
with some regular UITableViewCell
on it. And I know how to connect an UIDatePicker
to UITextField
. But now I would like to know if I could present the picker by tapping the whole cell even there is no text field on it.
What I tried is that I added a UIToolBar
as a subview on the picker and put code like [self showPicker]
in one of the UITableViewDelegate
methods: didSelectRowAtIndexPath
and then when I tapped the cell the picker showed like I wish.
But the problem is that when I clicked the UIBarButtonItem
button on the toolbar I put earlier, it didn't trigger its action even I added target and action like a normal button should have.
If you would like to see some code, here is the custom method showPicker
and regular stuff:
-(void)loadBirthdayPicker
{
CGFloat pickerWidth = kScreenWidth;
CGFloat pickerHeight = kScreenHeight/3;
CGFloat toolHeight = 40;
CGFloat toolWidth = kScreenWidth;
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(cancelButtonPressed:)];
UIBarButtonItem *flexibleSpaceMiddle = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(doneButtonPressed:)];
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, -toolHeight, toolWidth, toolHeight)];
[toolbar setItems:@[cancelButton, flexibleSpaceMiddle, doneButton]];
if (!_datePicker)
{
_datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, kScreenHeight, pickerWidth, pickerHeight)];
}
_datePicker.datePickerMode = UIDatePickerModeDate;
[_datePicker addSubview:toolbar];
[self.view addSubview:_datePicker];
[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
[_datePicker setFrame:CGRectMake(0, kScreenHeight-pickerHeight-60, pickerWidth, pickerHeight)];
}
completion:nil];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self loadBirthdayPicker];
}
-(void)doneButtonPressed:(UIBarButtonItem *)sender
{
NSLog(@"done");
}