2011-02-07 52 views
0

我有一个iPad应用程序,我有一个对象具有布尔类型的五个属性。我有一个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-5的可能值,那么你可以只使用该号码来选择图像:

NSString *imageName = [NSString stringWithFormat:@"imageForAttribute%d.png", attr]; 
myCell.imageView.image = [UIImage imageNamed:imageName];