UIPickerView不显示数据或调用viewForRow

时间:2014-12-08 07:45:14

标签: ios objective-c uipickerview

我在信息可用时动态地将数据添加到选择器视图。虽然我正在向我的数组添加数据并调用[picker reloadAllComponents],但是pickerview没有显示任何数据。

我还注意到以下两个代表都没有被调用:

- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{

这是我的viewDidLoad:

- (void)viewDidLoad
{
  [super viewDidLoad];
  // Do any additional setup after loading the view, typically from a nib.

  _pickerData = [[NSMutableArray alloc] init];
  _ActiveDevice = [[NSString alloc]init];

  // Connect data

  self.DevicePicker.dataSource = self;
  self.DevicePicker.delegate = self;

} 

这是加载我隐藏的PickerView的方法(我默认隐藏,直到按下按钮)

- (IBAction)FanDrop:(id)sender {
    [self printFromCoreData];
    [self.DevicePicker reloadAllComponents];
    if (_DeviceBox) _DeviceBox.hidden = !_DeviceBox.hidden;
}

我使用coredata中的内容加载数组作为DevicePicker的数据源这一切似乎都有效,因为我可以在记录数组中的内容时找到信息。此外,当单击接受按钮时,即使没有显示正确的数据也会被加载,因此告诉我除了代码的显示部分之外的所有内容都正常工作。

任何见解 - 我几乎可以尝试任何事情。

经过一次评论后,我意识到我忽略了在我的问题中包括代表们。所以他们在这里:

// The number of columns of data
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

// The number of rows of data
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return _pickerData.count;
}

// The data to return for the row and component (column) that's being passed in
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    //return _pickerData[row];
    return [_pickerData objectAtIndex:row];
}

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    return [_pickerData objectAtIndex:row];
}

以下是我添加数组pickerData的方法:

@interface CMTViewController ()
{
    NSMutableArray *_pickerData;
}

这是我用来加载_pickerData:

的CoreData方法
-(void)printFromCoreData{
    NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"DeviceData"];
    self.devices = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];


    for (NSManagedObject *info in _devices) {

    NSString *item = [NSString stringWithFormat:@"%1$@ %2$@",[info valueForKey:@"name"], [info valueForKey:@"ipAddress"]];

        if (![_pickerData containsObject:item]) {
            [_pickerData addObject:item];
        }
    }

     NSLog(@"_pickerData length %lu", (unsigned long)[_pickerData count]);
    [self.DevicePicker reloadAllComponents];
}

以下是按下风扇按钮时模拟器(或手机)中发生的情况的视图:

enter image description here

2 个答案:

答案 0 :(得分:3)

富士先生,

根据我们的对话,我的建议如下:

- (void)viewDidLoad
{
[super viewDidLoad];
 // Do any additional setup after loading the view, typically from a nib.
  [self printFromCoreData];
_ActiveDevice = [[NSString alloc]init];

// Connect data

self.DevicePicker.dataSource = self;
self.DevicePicker.delegate = self;
} 

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return _pickerData.count;
}

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

- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return _pickerData[row];
}

除非您想要更改布局并添加自定义标签等,否则请不要重复使用

答案 1 :(得分:0)

您需要添加以下方法

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

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return _pickerData.count;
}

 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:        (NSInteger)component{
    return  [_pickerData objectAtIndex:row];
   }
}