2011-02-07 7 views
0

私はiPadアプリケーションを持っていて、私はbool型の5つの属性を持つオブジェクトを持っています。私はUIImageViewを持っています。どこにこれらの属性が当てはまるかに応じて画像を配置する必要があります。これはうまく動作する私のソースコードです:Objective-C - ブール値の属性 - >画像をマップ

if (myObject.attribute1) { 
    myCell.imageView.image = [UIImage imageNamed:@"imageForAttribute1.png"]; 
} else if (myObject.attribute2) { 
    myCell.imageView.image = [UIImage imageNamed:@"imageForAttribute2.png"]; 
} else if (myObject.attribute3) { 
    myCell.imageView.image = [UIImage imageNamed:@"imageForAttribute3.png"]; 
} else if (myObject.attribute4) { 
    myCell.imageView.image = [UIImage imageNamed:@"imageForAttribute4.png"]; 
} else if (myObject.attribute5) { 
    myCell.imageView.image = [UIImage imageNamed:@"imageForAttribute5.png"]; 
} 

私のために、それは良くないようです。この "問題"を解決するためのより良い方法はありませんか?

最高のお付き合い

答えて

2

属性はすべて本当に独立していますか?表示されているロジックに基づいて、一度に1つの属性しか設定されないと予想しているようです。

そのような場合、多分属性はその後、あなただけの画像を選択し、その番号を使用することができ、1-5の可能な値で、単一のenum値に組み合わせることができる:

NSString *imageName = [NSString stringWithFormat:@"imageForAttribute%d.png", attr]; 
myCell.imageView.image = [UIImage imageNamed:imageName]; 
関連する問題