2013-04-23 21 views
0

我工作的一個自定義PickerView,我在選擇器中兩個組件(小時,分鐘),我已經取得了在會議紀要「0」組成的灰色,而不是可選擇的,一切工作正常除了行被重用...我的Minutes組件在灰色字體中顯示「0」(這是我想要的),但如果您滾動選擇器,我會看到「7,14,21 .....」灰色的字體!這裏是我的代碼如何停止UIPickerView重用

enter code here 
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view 
{ 

if(component == HOURS) { 
     NSString *s = [self.arrayOfHours objectAtIndex:row]; 
     return [pickerView viewForShadedLabelWithText:s ofSize:20 forComponent:0 rightAlignedAt:52 reusingView:view]; 

    } else if (component == MINUTES) { 
      NSString *s = [self.arrayOfMinutes objectAtIndex:row]; 
      return [pickerView viewForShadedLabelWithText:s ofSize:20 forComponent:1 rightAlignedAt:52 reusingView:view]; 
    } 
    else return 0; 
} 


    - (UIView *)viewForShadedLabelWithText:(NSString *)title ofSize:(CGFloat)pointSize forComponent:(NSInteger)component rightAlignedAt:(CGFloat)offset reusingView:(UIView *)view { 

//.........................  

    label = [self shadedLabelWithText:title ofSize:pointSize :component]; 

//.......................... 
} 



    - (UILabel *)shadedLabelWithText:(NSString *)label ofSize:(CGFloat)pointSize :(NSInteger)component 
{  

    UILabel *labelView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)]; 

    labelView.text = label; 

    if([label isEqual:@"0"] && component == 1) { 

     labelView.textColor = [UIColor grayColor]; 
    } 

    return labelView; 
    } 

1 - 有人可以幫我避免選擇器視圖重用行?

2 - 我如何使選擇器視圖顯示行在圓形/圓形?

+0

您發佈的代碼包含名爲'shadedLabelWithText:ofSize ::'的方法,但您的'viewForRow:'方法調用名爲'viewForShadedLabelWithText:ofSize:forComponent:rightAlignedAt:reusingView:'的方法。 – rmaddy 2013-04-23 23:50:02

+0

對不起,我編輯了我的問題......謝謝! – DevCali 2013-04-24 02:00:34

回答

0

這是代碼造成你的麻煩:

if([label isEqual:@"0"] && component == 1) { 
    labelView.textColor = [UIColor grayColor]; 
} 

你的問題是,由於行得到重用,當你設置一排灰色,在它熄滅堆棧,它會有時回來。當它確實回來時,它的標籤顏色將會是仍然是會變成灰色。

所以,代碼應改爲是這樣

if([label isEqual:@"0"] && component == 1) { 
    labelView.textColor = [UIColor grayColor]; 
} else 
{ 
    labelView.textColor = [UIColor blackColor]; // Or whatever color it normally is. 
} 

你對使它圓形問題,你會行數設置爲像10000(一些用戶一些瘋狂的數字將永遠不會滾動)。然後將選取器的位置設置到10K的中間位置。然後,您需要有一個數組,其中包含要在「永不停止」的選取器中顯示的所有值。

你會再使用模運算符(%)來檢查將您的陣列的count由當前行的剩餘部分。例如:

-(UIView *)somePickerWantsViewForRow:(int)row 
{ 
    ... 
    NSString *titleForRow = [self.someArray objectAtIndex:(self.someArray.count % row)]; 
    pickerRow.titleLabel.text = titleForRow; 
} 

在這個例子中,someArray可能是('cat','dog','farmer','pizza')

+0

感謝您的回覆,撤消..我試過第一個解決方案,但它沒有工作:(我現在會嘗試循環解決方案... – DevCali 2013-04-24 03:40:41

+0

@Ahmad文字顏色變化? – Undo 2013-04-24 03:41:49

0

我剛剛想出了「重用行」的問題,這是因爲在下面的方法與「無」取代「視圖」一樣簡單。

返回[pickerView viewForShadedLabelWithText:■ofSize:20 forComponent:1 rightAlignedAt:52 reusingView:];

我花了幾個小時才弄明白這一點。哇,難怪我喜歡編程! :)