2012-02-22 15 views
3

私はいくつかのUIPickerViewをviewControllerに持っています。複数のUIPickerView

これは、1つのピッカー行に2つのUILabelsを表示するため、カスタマイズする必要があります。

そして私は、これらのデリゲートメソッドを使用します。

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row 
forComponent:(NSInteger)component { 
    // this method is use for normal pickers, and I would judge whether the picker that calling 
    // this method is a normal one or the special one by pickerView. 
} 
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row 
forComponent:(NSInteger)component reusingView:(UIView *)view { 
    // and this one is specially for the special picker, I would also judge the pickerView is 
    //normal or special, if pickerView is normal, return nil, else I return a UIView with 
    //2 UIlabels. 
} 

しかし、今、私のデバッグの後、私は一緒に2つの方法を実装する場合、二つ目は常に呼ばれていることを発見し、最初のものが呼び出されないことを決して思わ、

となり、私の特別なピッカーは正しいデータを表示しますが、他には何もありません。

どうすればいいですか?

2番目の方法ですべてのピッカーのデータを与えると、特別なピッカーのreusingViewは他のピッカーと同じ形式ではないので、reusingViewは問題になりますか?

ありがとうございます!

+0

興味深いのは... 2番目の方法では他のピッカーのためにnilを返しますか? – govi

+0

はい、それは問題でしょうか?私が何かを返さなければ、警告が出るので、すぐにそのコードを削除しようとします。 –

+0

よく分かりません。あなたはその行を削除する必要はありません、それは完璧です。 – govi

答えて

1

別の代理人を使用することはそれほど簡単ではありません。別のクラスを作成し、

@interface MyCustomDelegate : NSObject <UIPickerViewDelegate> 

とデリゲートメソッドを実装し、この場合は

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row 
forComponent:(NSInteger)component { 
    // this method is use for normal pickers, and I would judge whether the picker that calling 
    // this method is a normal one or the special one by pickerView. 
} 

あなたがこれを行うと、このクラスのインスタンスを作成し、...

などのデリゲートとして設定
pickerView.delegate = [[MyCustomDelegate alloc] initWithData:data] autorelease]; 

ここで、データは、タイトルのデリゲートメソッドで使用するために含めることができます。

すべてのピッカーに対して同じデリゲートインスタンスを使用することも、別々のインスタンスを作成することもできます。あなたが持っているデータの種類に大きく依存します。それらがお互いに無関係な場合は、別々のインスタンスを使用するといいでしょう。

それはそうです。あなたは勿論この

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

    } 
    else { 
     UILabel *lbl = [[UILabel alloc] init]; 
     lbl.text = [self pickerView:pickerView titleForRow:row forComponent:component]; 
     //do some more styling like setting up the font as bold 
     //adding some padding to the text and some shiny things 
     return lbl; 
    } 
} 

ようなものをやっているだろう 秒1、については


、既存のクラスでこれを行うことになります。

1

私はさらに 'govi'によってABOVE応答に追加します。 'tag'プロパティ(これは、各UIPickerビューに異なる 'タグ'値を割り当てる必要があります.1,2,3 ...など)を使用して複数のUIPikeViewを識別することもできます。例えば、

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { 
if (pickerView.tag ==0) { 

} 
else if (pickerView.tag ==1) { 
    UILabel *lbl = [[UILabel alloc] init]; 
    lbl.text = [self pickerView:pickerView titleForRow:row forComponent:component]; 
    //do some more styling like setting up the font as bold 
    //adding some padding to the text and some shiny things 
    return lbl; 
} 

}

は、さらにより多くのあなたがこれらの

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { if (pickerView.tag ==0) { } 
else if (pickerView.tag ==1) { } } 

のいずれかを使用することができます。これは、uはあなたピッカーは、単にいくつかの値を表示したい場合に特に使用されるものです。表示されたラベル(つまり、幅、高さ、フォントなど)にいくつかのカスタマイズを追加する場合は上記のものが使用されます。