2

UIPickerViewフェッチの結果を入力しようとしています。できました。重複があるため、次のコードを使用して個別のレコードと必要なプロパティのみを取得することにしました。このコードは、配列が正しいデータを持っているという点で正常に機能し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

次のようなコード スニペットを使用して@distinctUnionOfObjects、結果ディクショナリ内の特定のキー (例: endCalYear) に対して個別の結果 ( )を取得することをお勧めします。

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

pickerView:numberOfRowsInComponent:で行数を返すことができ、次の distinctYears.countようなpickerView:titleForRow:forComponent:コード スニペットを使用して各タイトルの文字列を返すことができます。

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

と同様に、ピッカーにデータを提供するおよびプロトコル メソッドをUITableView処理する必要があります。詳細については、リンクされたドキュメントを参照してください。UIPickerViewDataSourceUIPickerViewDelegate

于 2012-10-23T20:37:19.593 に答える