2012-02-22 47 views
3

我有我的viewController幾個UIPickerViews。幾個UIPickerView

這是其中的一個需要定製,因爲它在1個選取器行中顯示2個UILabel。

而且我使用這些委託方法:

- (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種方法一起,第二個總是被調用,第一個似乎從未被稱爲,

它導致我的特殊選擇器顯示正確的數據,但其他人什麼也沒有。

我該怎麼做?

如果我在第二種方法中給出所有采集器的數據,那麼reusingView會成爲一個問題,因爲特殊採樣器的reusingView與其他採集器的格式不同?

非常感謝!

+0

有趣...你是否在第二種方法中返回其他採樣器的nil? – govi 2012-02-22 11:13:32

+0

是的,那會是問題嗎?如果我不返回任何內容,將會有警告,所以...我會立即嘗試刪除該代碼。 – 2012-02-22 11:17:20

+0

好..沒有。你不必刪除那條線,這很好。 – govi 2012-02-22 11:24:56

回答

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; 
    } 
} 

東西Ofcourse,你會在你現有的類來這樣做。

1

我會進一步加入'govi'的ABOVE迴應; 'tag'屬性(爲此,您必須爲每個UIPicker視圖指定不同的'tag'值,即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想你選取器來簡單地顯示一些值。而如果你想添加一些定製到顯示的標籤(即寬度,高度,字體...),則使用上面的一個。)