2012-03-21 13 views
2

3つのコンポーネント(または列)を持つUIPickerViewがあり、各列の行またはアイテムの量が異なります。私は、各行の背景色を設定できる必要があります。お菓子上記Xcode:UIPickerView個々の行の背景色を変更する

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

CGRect imageFrame = CGRectMake(0.0, 0.0, 15, 15); 
UIImageView *label = [[[UIImageView alloc] initWithFrame:imageFrame] **autorelease**]; 

if (row == 0) 
{ 
    label.backgroundColor = [UIColor redColor]; 
} 
if (row == 1) 
{ 
    label.backgroundColor = [UIColor blueColor]; 
} 
if (row == 2) 
{ 
    label.backgroundColor = [UIColor blackColor]; 
} 
return label; 
} 

コードのすべてのコンポーネント/列と同じに、各行に同じ色の順序を割り当てます:私は私が必要とするまさにいくつかの掘削を行なったし、ほとんどの作品に何かを見つけましたが、ありません。そう例えば:

列1はあります:赤/青/黒 2列はあります:赤/青/黒 列3があります:黒/赤/青私ができるようにする必要が

各列に固有のカラーシーケンスを使用します。私は、各列が上記の色の同じシーケンスを持つ必要はありません。私は理解していた場合は赤/白

答えて

0

カラム1:黒/緑/赤 コラム2:黄/紫/赤/青 列3だから、例えば私は次のようなものを持っていることあなたは正しく、コンポーネントをオンにする必要があります。たとえば、次のように少し長くて扱いにくい

- (UIView *) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row 
      forComponent:(NSInteger)component reusingView:(UIView *)view { 
    CGRect imageFrame = CGRectMake(0.0, 0.0, 15, 15); 
    UIImageView *label = [[[UIImageView alloc] initWithFrame:imageFrame] **autorelease**]; 

    switch (component) { 
     //I'm using enumeration types for the components because its pretty 
     case THIRD_COMPONENT: 
      if (row == 0) 
      { 
       label.backgroundColor = [UIColor whiteColor]; 
      } 
      if (row == 1) 
      { 
       label.backgroundColor = [UIColor redColor]; 
      } 
      break; 
     case SECOND_COMPONENT: 
      if (row == 0) 
      { 
       label.backgroundColor = [UIColor blueColor]; 
      } 
      if (row == 1) 
      { 
       label.backgroundColor = [UIColor purpleColor]; 
      } 
      if (row == 2) 
      { 
       label.backgroundColor = [UIColor redColor]; 
      } 
      if (row == 3) 
      { 
       label.backgroundColor = [UIColor yellow]; 
      } 
      break; 
     case FIRST_COMPONENT: 
      if (row == 0) 
      { 
       label.backgroundColor = [UIColor redColor]; 
      } 
      if (row == 1) 
      { 
       label.backgroundColor = [UIColor blueColor]; 
      } 
      if (row == 2) 
      { 
       label.backgroundColor = [UIColor blackColor]; 
      } 
      break; 
    } 

    return label; 
} 

そのしかし、その打撃を与えると、それはあなたの問題を修正するかどうかを確認します。それぞれのコンポーネントの配列にbackgroundColorを設定してラベルを保存することをお勧めします。私はあなたが私のアプリの一つで記述しているし、それは私がそれをやった方法だったものに似た何かをしなければならなかった

switch (component) { 
    case THIRD_COMPONENT: 
     label = [thirdBandColorsArray objectAtIndex:row]; 
     break; 
    case SECOND_COMPONENT: 
     label = [secondBandColorsArray objectAtIndex:row]; 
     break; 
    case FIRST_COMPONENT: 
     label = [firstBandColorsArray objectAtIndex:row]; 
     break; 
} 

:代わりの文が新しいswitch文は次のようになります場合は、それらのすべてを持っていることのその方法。私が言ったように、それは単なる提案です。お役に立てれば。

0
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view 
{ 
    UILabel *label = (UILabel*) view; 
    if (label == nil) { 
     label = [[UILabel alloc] init]; 
    } 

    label.textColor = [UIColor whiteColor]; 
    label.backgroundColor = [UIColor setAnyRandomColor]; 
    CGSize rowSize = [pickerView rowSizeForComponent:component]; 
    CGRect labelRect = CGRectMake (0, 0, rowSize.width, rowSize.height); 
    [label setFrame:labelRect]; 

    return label; 
}