2015-10-18 87 views
-1

我現在有2個採摘設置爲我viewController.h2選擇器不顯示值

@interface viewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate> 

@property (weak, nonatomic) IBOutlet UIPickerView *lPicker; 
@property (weak, nonatomic) IBOutlet UIPickerView *rPicker; 

@end 

我lPicker.tag = 0,rPicker.tag = 1

在爲我的viewController實現文件。米

我定義了以下方法...

NSArray *data1 = {@"one", @"two", @"three" }; 
NSArray *data2 = [NSArray arrayWithObjects: [UIImage imageNamed:@"img1.png"],[UIImage imageNamed:@"img2.png"], nil]; 

-(NSInteger) numberOfComponentsInPickerView:(UIPickerView*)pickerView { 
    return 1; //both contain only 1 column 
} 

-(NSInteger) pickerView:*UIPickerView *) pickerView numberOfRowsInComponent:(NSInteger)component { 
    if(pickerView.tag == 0) return data1.count; if(pickerView.tag == 1) return data2.count; 
} 

我現在有點與followi麻煩ng,在lPicker上我想顯示data1的值,在右邊的picker中我想顯示data2的值。

我試着創建方法 - (id)pickerView但不能返回NSString *和UIImageView在同一時間。

如果我實施類似不工作...

-(id) pickerView:(UIPickerView *) pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *) view { 
    if(component == 0) { 
     UILabel *label = [UILabel alloc]; 
     label.text = [data objectAtIndex:row]; 
     [view addSubview:label]; 
     return view; 
    } 
    if(component == 1) { 
     UIImageView *image = [[UIImageView alloc] initWithImage: [data2 objectAtIndex:row]]; 
     [view addSubview: image]; 
     return view; 
    } 
    return view; 
} 
+0

如何使用兩個不同的對象,作爲這兩個選擇器視圖的委託/數據源?那麼你不會迷惑自己。 – matt

+0

「,但不能同時返回NSString *和UIImageView。」不,但它可以同時返回包含string_和圖像視圖的_a標籤。 – matt

+0

這是一個錯字嗎?最後一個方法不應該是'numberOfRowsInComponent:'。 – rmaddy

回答

1

截至此刻寫的,你所做的關於重用視圖壞的假設。您還需要更改如何確定正在使用哪個選取器。並修復方法的返回值。

試試這個:

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { 
    if (pickerView.tag == 0) { 
     UILabel *label = (UILabel *)view; 
     if (!label) { 
      [[UILabel alloc] init]; 
     } 
     label.text = data1[row]; 
     [label sizeToFit]; 

     return label; 
    } else { 
     UIImageView *image = (UIImageView *)view; 
     if (!image) { 
      image = [[UIImageView alloc] init]; 
     } 
     image.image = data2[row]; 
     [image sizeToFit]; 

     return image; 
    } 
} 
+0

謝謝,這澄清了很多。對不起,我的問題令人困惑,我只用xcode進行了2天的編碼。 – Steveo90