开发者

UIDatePicker and a UITableView

I have a UITableViewController that is the detail view for another UITableViewController.

On this table is a cell labeled "Date". Using Apple's "DateCell" example (http://developer.apple.com/library/ios/#samplecode/DateCell/Introduction/Intro.html), I have tried to add a UIDatePicker when you touch the cell.

I have wired up everything exactly the way the example does. The only thing I've changed is that I fenced the didSelectRowAtIndexPath code in this:

if(indexPath.section == 1 && indexPath.row == 0)
{
   //Date picker stuff
}
开发者_开发问答

This is to ensure it only runs when the right cell is pressed.

For the life of me, I can't get it to work. Clicking it does nothing but turn it blue (selected). Clicking it repeatedly does nothing.

If I scroll to the bottom of the UITableView, clicking it animates up a white rectangle. Clicking repeatedly eventually covers the whole table view.

It doesn't make a lot of sense to me. Please..how can I do this?

If you want more code, I can provide it, but it's virtually identical to the DateCell code. I copied/pasted for the most part.

Thanks in advance,

Clif


Make sure you have a picker object in IB if that's what you are using, then create an IBOutlet reference and connect it to the IB object. I set my pickerView to hidden in IB and make it visible when required. Otherwise you can simply instantiate one as needed.

In your didSelectRowAtIndexPath, you can try the code below and see what happens.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (**your cell/section selection logic here**) {
        [self.view endEditing:YES]; // resign firstResponder if you have any text fields so the keyboard doesn't get in the way
        [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES]; // Scroll your row to the top so the user can actually see the row when interacting with the pickerView

        // Pickerview setup
        [self.typePicker setCenter:CGPointMake(150, 500)]; // place the pickerView outside the screen boundaries
        [self.typePicker setHidden:NO]; // set it to visible and then animate it to slide up
        [UIView beginAnimations:@"slideIn" context:nil];
        [self.typePicker setCenter:CGPointMake(150, 250)];
        [UIView commitAnimations];        
    }
}

After that you need to implement your pickerView:didSelectRow: method if you want to update the label of your cell as the picker view selection changes...

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
{
    // Your code to get the current table view cell and update it with the data from your pickerView
}

Make sure your viewController is declared as delegate for the tableView <UITableViewDelegate> as well as for the pickerView `'

This should give you a good head start. Let me know if you have any questions etc.

Cheers, Rog

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜