我有一个包含 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”被打印到日志中。任何建议将不胜感激。提前致谢!