2

我正在尝试UIPickerView使用 fetch 的结果填充 a。我能够做到。由于有重复,我决定使用下面的代码来获取不同的记录和只需要的属性。该代码工作正常,因为数组array1&dictlevel1具有正确的数据(如下所示)。我无法弄清楚如何将其array1放入选择器视图中?

    NSError *error;
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Factors" inManagedObjectContext:managedObjectContext];

    [fetchRequest setEntity:entity];
    [fetchRequest setResultType:NSDictionaryResultType];
    [fetchRequest setReturnsDistinctResults:YES];
    [fetchRequest setPropertiesToFetch:@[@"level1"]];

    self.title = @"Factors";
    array1 = [[NSArray alloc] init];
    array1 = [managedObjectContext executeFetchRequest:fetchRequest error:&error]; 
    NSDictionary *dictlevel1 = [array1 dictionaryWithValuesForKeys:[NSArray arrayWithObject:@"level1"]];

    NSLog(@"%@", array1[1]);
    NSLog(@"%@", [dictlevel1 dictionaryWithValuesForKeys:[NSArray arrayWithObject:@"level1"]]);

这是输出NSLog

2012-10-23 14:55:47.774 CoreData2[1477:c07] (
        {
        level1 = "External Combustion Boilers";
    },
        {
        level1 = "Internal Combustion Engines";
    },
        {
        level1 = "Industrial Processes";
    },
        {
        level1 = "Petroleum and Solvent Evaporation";
    },
        {
        level1 = "Waste Disposal";
    },
        {
        level1 = "Stationary Source Fuel Combustion";
    },
        {
        level1 = "Miscellaneous Area Sources";
    },
        {
        level1 = "Mobile Sources";
    },
        {
        level1 = "Solvent Utilization";
    },
        {
        level1 = "Storage and Transport";
    },
        {
        level1 = "Waste Disposal, Treatment, and Recovery";
    },
        {
        level1 = "Natural Sources";
    }
)

2012-10-23 14:55:47.775 CoreData2[1477:c07] {
    level1 =     (
        "External Combustion Boilers",
        "Internal Combustion Engines",
        "Industrial Processes",
        "Petroleum and Solvent Evaporation",
        "Waste Disposal",
        "Stationary Source Fuel Combustion",
        "Miscellaneous Area Sources",
        "Mobile Sources",
        "Solvent Utilization",
        "Storage and Transport",
        "Waste Disposal, Treatment, and Recovery",
        "Natural Sources"
    );
}
4

2 回答 2

0

与 a 非常相似UITableView,您必须处理UIPickerViewDataSourceUIPickerViewDelegate协议方法,这些方法为选择器提供数据。有关详细信息,请参阅链接的文档。

于 2012-10-23T20:37:19.593 回答
0

我建议使用这样的代码片段来获得@distinctUnionOfObjects结果字典中给定键(例如 endCalYear)的不同结果():

NSArray *distinctYears = [periodsFilteredByDates
                valueForKeyPath:@"@distinctUnionOfObjects.endCalYear"];

然后,您可以使用和pickerView:numberOfRowsInComponent:返回行数, 您可以使用这样的代码片段为每个标题返回一个字符串:distinctYears.countpickerView:titleForRow:forComponent:

NSNumber *endCalYear = distinctResults[row];
NSString *rowTitle = [NSString stringWithFormat:@"%@",endCalYear];
return rowTitle;
于 2012-11-04T11:09:10.500 回答