UITableViewCell 中的 UIPickerView 不加载数据

UIPickerView in UITableViewCell doesn't load data

我有一个带有 UITableViewDelegate 的 UITableView

其中一个 CellType 由一个标签和一个 UITextField.

我已经将 UITextFieldinputView 设置为 UIPickerView

UIPickerView 及其委托是在 UITableViewDelegate

cellForRowAtIndexPath 函数中构造的

当我显示 table 时,会创建 table 行,当我单击 UITextField 时,会弹出一个选择器,但是 没有填充选择器 并且我放入委托中的日志没有显示。

到目前为止,我已经尝试了一些方法,比如添加额外的刷新,但到目前为止没有结果。

由于日志没有显示,我怀疑对委托和数据源的引用有问题? PickerView 的实例是否在某处被破坏?也许我忘了实现一个我没有看到的方法?

understanding/fixing 中的任何帮助,我们将不胜感激!

截图:

在我的代码中,它看起来像这样:

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

我的 ListPickerViewDelegate 看起来像这样:

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

UIPickerView 上的 delegatedatasource 属性已 weak 定义。您的 ListPickerViewDelegate 对象是在 createListViewCell:tableView: 方法的范围内定义的,因此它永远不会被保留。

尝试在 TableViewDelegate 中为 ListPickerViewDelegate 创建一个强大的 属性 并在其他地方初始化它(也许在重载的 init 方法中)。然后只需将 UIPickerView delegatedatasource 分配给此 属性.