2010-09-29 37 views
1

這似乎是直接從文檔非常標準的代碼,所以我不知道我做錯了......選擇器不能識別的最後一排

我的三分量選擇器看起來不正確時它加載時,選擇指示器在拾取器上放置得太低(所以灰色條被正確放置,但明顯的凸起的覆蓋層太低)。

然後,當我選擇一行時,除非選擇最後一行,否則它會完美地工作,在這種情況下,它認爲我選擇了倒數第二行。

數組中的標題數是正確的(例如7),當我選擇行「0-5」時,它的效果很好,但是當我選擇第6行(第7行)時,它仍然我認爲我選擇了「5」。因此,第六行和第七行將返回「5」而不是「5和6」。

這發生在所有這三個組件上,並且實際上是以類似的方式發生在代碼中其他地方的另一個更簡單的採集器中(實際上,只有當我來自父視圖控制器時,才使用該採集器。我來自子視圖控制器它沒事)。

你能幫忙嗎?

- (void)loadmypicker { 
UIPickerView *mypicker = [[UIPickerView alloc] init]; 
[mypicker setDataSource:self]; 
[mypicker setDelegate:self]; 
pickerTitles_Dil = [[NSArray alloc] initWithObjects:@"0", @"1", @"2", @"3", @"4", @"5", @"6", nil]; 
pickerTitles_Eff = [[NSArray alloc] initWithObjects:@"a", @"b", @"c", @"d", @"e", @"f", @"g", @"h", @"i", @"j", @"k", nil]; 
pickerTitles_Stat = [[NSArray alloc] initWithObjects:@"A", @"B", @"C", @"D", @"E", @"F", @"G", nil]; 
[mypicker setShowsSelectionIndicator:YES]; 
[mypicker selectRow:0 inComponent:0 animated:NO]; 
[mypicker selectRow:0 inComponent:1 animated:NO]; 
[mypicker selectRow:0 inComponent:2 animated:NO]; 
[mypicker setFrame:CGRectMake(0, 98, 320, 253)]; 
[self addSubview:mypicker]; 
    [mypicker release]; 
} 

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { 

if (component == 0) 
    return [pickerTitles_Dil objectAtIndex:row]; 
else if (component == 1) 
    return [pickerTitles_Eff objectAtIndex:row]; 
else return [pickerTitles_Stat objectAtIndex:row]; 
} 

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { 
return 3; 
} 


- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { 
if (component == 0) 
{return [pickerTitles_Dil count]; 
} 
else if (component == 1) 
{return [pickerTitles_Eff count]; 
} 
else 
{return [pickerTitles_Stat count]; 
} 
    } 

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ 
NSLog (@"row: %d, component :%d", row, component); 
} 

謝謝你的幫忙!

有沒有其他人有這個問題?總之,選取器顯示最後一行,但我不能選擇它。謝謝!

+0

我可以建議你重新標記以包含語言/應用程序/ ???這是指 – Basic 2010-09-30 16:36:48

+0

當然......這有幫助嗎?謝謝! – snorkelt 2010-09-30 18:32:07

回答

0

不知何故我認爲下面的數據源方法已被廢棄,對嗎?

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 

如果您嘗試使用以下方法,會發生什麼?

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view 

也許這會有幫助嗎?但我不確定這會爲您解決問題。

編輯:嘗試這樣的事情,新的方法不能直接返回字符串,但需要訪問量:

/* Because the UIPickerView expects a UIView for every row you insert in the UIPickerView, you need to make one. What I do here is really simple. I create a UILabel* and with each row it requests I just change the text in the UILabel. */ 

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view 
    { 
    /* Create a UILabel and set it 40 pixels to the right, to make sure it is put nicely in the UIPickerView */ 
     UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(40, 0, 280, 25)] autorelease]; 
     label.textColor = [UIColor blackColor]; 
     label.backgroundColor = [UIColor clearColor]; 

     switch (row) 
     { 
      default: 
      case kUnderClock: 
       label.text = @"Under clock"; 
       break; 
      case kAboveSlider: 
       label.text = @"Above lock slider"; 
       break; 
      case kCenter: 
       label.text = @"Vertically centered"; 
       break; 
     } 

     return label; 
    } 

很抱歉的壓痕問題,這個複製直接從我的博客。

+0

感謝您的迴應!當我用viewForRow替換titleForRow行時,我得到一個[NSCFString superview]:無法識別的選擇器發送到實例...我沒有看到titleForRow在Apple Dev上被棄用,但是有沒有辦法讓這個工作成功? – snorkelt 2010-10-02 01:19:50

+0

好吧,我改變了方法viewForRow(花了我一會兒,因爲3個組件和很多標籤),但它仍然沒有選擇最後一行(如果我的標籤從0-6,我只能選擇降至5)。此外,選擇器的選擇指示器太低,所以我認爲問題在我的代碼中的其他地方。所選的值是灰色的值,透明覆蓋條低於此值。那可能是什麼? – snorkelt 2010-10-02 15:31:08

+0

您是否嘗試將UIPickerView放置在0x0位置,以查看它如何反應? – 2010-10-02 18:13:22

1

問題修正:拾取器高度不能超過216個。因爲我在253時過去了,所有其他問題都隨之而來。謝謝!

相關問題