1

我有一个包含 Picker 的自定义 UITableViewCell 类。单元格及其内容由核心数据实体填充。由于这个自定义单元格可以在多个视图控制器中查看,因此我有一个实用程序类,我希望它必须处理构造、数据源和委托方法。

我似乎遗漏了一些东西,因为当我将数据源设置为 self 时,单元格会正确显示。当我将它设置为 Utility 类时,只有该numberOfComponentsInPickerView:方法被调用,然后应用程序崩溃,日志中除了 (lldb) 之外没有任何内容。对于我的代码示例,我将创建一个简单的选择器,其中包含一个组件和一行标题为“测试”。

ViewController.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PickerCell *pCell = nil;
    Picker *picker = [self.array objectAtIndex:indexPath.row];
    UtilityCustomCells *cellUtility = [[UtilityCustomCells alloc]init];
    pCell = [cellUtility createPickerCell:tableView withReuseIdentifier:kPickerCellRID andPicker:picker];
    pCell.picker.delegate = cellUtility;  //does not work
    pCell.picker.dataSource = cellUtility;  //does not work
    /* This works
    pCell.picker.delegate = self;
    pCell.picker.dataSource = self;
    */
    return pCell;
    }
}

并且在

utilityCustomCells.h

#import "PickerCell.h"
#import "Picker.h"

@protocol CustomCellUtilityDelegate <NSObject,UIPickerViewDataSource,UIPickerViewDelegate>
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;

@end

最后:

UtilityCustomCells.m

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    NSLog(@"Here1");
    return 1;
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    NSLog(@"Here2");
    return 1;
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSLog(@"Here3");
    NSString *title = @"Test";
    return title;
}

只是为了重新迭代,只有“Here2”被打印到日志中。任何建议将不胜感激。提前致谢!

4

1 回答 1

0

我能够弄清楚这一点。我UtilityCustomCell在. cellForRowAtIndexPath_ ViewController.m事实上,我需要将它作为一个实例变量ViewController.m和 alloc/init in viewDidLoad。所以现在:

ViewController.m
@interface AssetFormVC ()
{
    UtilityCustomCells *cellUtility;
}

@end

- (void)viewDidLoad
{
    [super viewDidLoad];
    cellUtility = [[UtilityCustomCells alloc]init];
    cellUtility.pickerDataSource = [self configurePickerData];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PickerCell *pCell = nil;
    Picker *picker = [self.array objectAtIndex:indexPath.row];
    pCell = [cellUtility createPickerCell:tableView withReuseIdentifier:kPickerCellRID andPicker:picker];
    pCell.picker.delegate = cellUtility; 
    pCell.picker.dataSource = cellUtility; 
    return pCell;
    }
}

我现在可以通过属性将带有选择器数据源的字典发送到 cellUtilityUtilityCustomCells.h

@property (nonatomic,strong) NSDictionary *pickerDataSource;

于 2014-10-15T16:55:10.173 回答