UIDatePicker and UITableView

I have UITableViewController, this is a detailed view for another UITableViewController.

In this table is a cell labeled "Date." Using the Apple example “DateCell” ( http://developer.apple.com/library/ios/#samplecode/DateCell/Introduction/Intro.html ), I tried to add UIDatePickerwhen you touch the cell.

I connected everything as the example does. The only thing that I changed is that I robbed the code didSelectRowAtIndexPathas follows:

if(indexPath.section == 1 && indexPath.row == 0)
{
   //Date picker stuff
}

This means that it only works when you click the right cell.

In life, I cannot make it work. Clicking on it does nothing except that it is blue (selected). Pressing it again does nothing.

If I scroll to the bottom of a UITableView, clicking on it enlivens the white rectangle. When you click on the screen again, the entire view of the table opens.

It doesn't really matter to me. Please ... can I do this?

If you need more code, I can provide it, but it is almost identical to DateCell code. I copied / pasted for the most part.

Thanks in advance,

Cliff

+3
source share
1 answer

Make sure you have a select object in IB, if that is what you are using, then create an IBOutlet link and connect it to the IB object. I put my pickerView hidden in IB and make it visible when needed. Otherwise, you can simply create an instance if necessary.

didSelectRowAtIndexPath , .

- (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];        
    }
}

pickerView:didSelectRow:, ...

- (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
}

, viewController tableView <UITableViewDelegate>, pickerView `'

. , ..

Cheers,

+6

Source: https://habr.com/ru/post/1792711/


All Articles