Sly
Sly

Reputation: 733

Static UITableViewController with UIPickerView fails to load UIPickerView

I'm configuring a UIPickerView inside a static UITableViewCell via the Storyboard. For some reason, the UIPickerView does not appear when loading the Controller.

Storyboard: Storyboard

The UIPickerView is a child of the Cell Content View...and the Table View is configured to display "Static Cells". (Grouped style)

The UIPickerView delegate and dataSource are connected to the UITableViewController as Outlets in the Storyboard Connections Inspector:

Connections Inspector

If I programmatically add the Picker View to the Table Cell, in the viewDidLayoutSubviews callback, then the PickerView appears and the delegate callbacks are fired so that the Picker can be configured with data. (titleForRow, etc):

In the TableViewController.m:

    // Can't figure out why - but regionPicker view can't be added in viewDidLoad, so it gets added here
- (void) viewDidLayoutSubviews {
    // For some reason regionPicker view gets pushed to top of view unless setting x & y to 0,0 here...
    [self.regionPicker setFrame:CGRectMake(0, 0, 320, 162)];
    // Add the regionPicker view as a subview to the regionPickerCell
    [self.regionPickerCell.contentView addSubview:self.regionPicker];    
}

This causes problems with proper initialization of the Picker, because viewDidLayoutSubviews gets called every time a new selection is made in the UIPIckerView.

Here's what my TableViewController.h file looks like:

@import UIKit;

@class DetailViewController;

@interface DetailViewController : UITableViewController<UIPickerViewDataSource, UIPickerViewDelegate>

@property (weak, nonatomic) IBOutlet UITableViewCell *regionCell;
@property (weak, nonatomic) IBOutlet UITableViewCell *regionPickerCell;
@property (weak, nonatomic) IBOutlet UIPickerView *regionPicker;

@end

What's the proper way to load a UIPickerView in a Static UITableViewContoller via the Storyboard?

Also, I don't know if this is relevant, but the Table View Controller, in this case, is getting loaded via a segue, from another UITableViewController:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"configNotification"]) {

        UINavigationController *navigationController = segue.destinationViewController;
        // Here's the UITableViewController in question...
        DetailViewController *detailViewController = [navigationController viewControllers][0];

        NSIndexPath *selectedPath = [self.tableView indexPathForCell:sender];
    }
}

Any thoughts are greatly appreciated!

Xcode v6.1, SDK v8.1

Upvotes: 1

Views: 744

Answers (1)

Sly
Sly

Reputation: 733

I resolved this by deleting the existing picker views in this controller, from the Storyboard, and then adding each one back.

For some reason, the Picker View was greyed out in the Storyboard view. (first picture above) After deleting the Picker View and adding it back it was not greyed out. I don't know why the Picker View was greyed out in the first place, or what that signifies from the Storyboard, but hopefully this will help someone in the future.

Upvotes: 0

Related Questions