Search code examples
iosobjective-ciphoneuitableviewuipickerview

UIPickerView in UITableViewCell doesn't load data


I have a UITableView with a UITableViewDelegate.

One of the CellTypes consist of a label and a UITextField.

I've set the inputView of the UITextField to a UIPickerView.

The UIPickerView and its delegate are constructed in the cellForRowAtIndexPath function of the UITableViewDelegate

When I display my table, the tablerows are created, when I click on the UITextField, a Picker pops up however the picker is not populated and the Logs I've put in the Delegate are not showing.

So far I've tried a few things, like adding an extra refresh but so far without result.

Since the logs aren't showing I'm suspecting there is something wrong with the reference to the delegate and datasource? Is the instance of PickerView destroyed somewhere? Maybe I forgot to implement a method I'm not seeing?

Any help in understanding/fixing this is appreciated!

Screenshot:

screenshot

In my code this looks like this:

TableViewDelegate.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = nil;
    cell = [self createListViewCell:indexPath tableView:tableView];
    return cell;
}

- (UITableViewCell *) createListViewCell:(NSIndexPath *)indexPath tableView:(UITableView *)tableView {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:LIST_CELL_ID];
    ListTableCell *listTableCell = (ListTableCell *) cell;
    List *list = [listsData objectAtIndex:indexPath.row];

    //Set List Label
    listTableCell.listLabel.text = list.name;

    //Create Value picker
    UIPickerView *picker = [[UIPickerView alloc]init];
    //Create Delegate for picker
    ListPickerViewDelegate *pickerDelegate = [[ListPickerViewDelegate alloc]init];

    pickerDelegate.data=list.itemData;

    picker.dataSource = pickerDelegate;
    picker.delegate = pickerDelegate;

    listTableCell.listTextValue.inputView = picker;
    [picker reloadAllComponents];

    return cell;
}

And my ListPickerViewDelegate Looks like this:

ListPickerViewDelegate.h

#import <UIKit/UIKit.h>

@interface ListPickerViewDelegate:NSObject <UIPickerViewDelegate,UIPickerViewDataSource>

@property (nonatomic) NSArray *data;
@property (nonatomic) UITextField *textField;

@end

ListPickerViewDelegate.m

#import "ListPickerViewDelegate.h"
#import "DataItem.h"

@implementation TagListPickerViewDelegate
@synthesize data;

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    DataItem *thisDataItem = [data objectAtIndex:row];
    NSLog(@"Value for row is %@", thisDataItem.name ); //Log not showing

    return DataItem.name;
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    NSLog(@"Count for data is %li", (unsigned long)[data count]); //Log not showing
    return [data count];
}
@end

Solution

  • The delegate and datasource properties on a UIPickerView are weak defined. Your ListPickerViewDelegate object is defined within the scope of the createListViewCell:tableView: method, so it is never retained.

    Try creating a strong property inside your TableViewDelegate for the ListPickerViewDelegate and initialising it somewhere else (maybe in an overloaded init method). Then just assign the UIPickerView delegate and datasource to this property.