将图片以动态方式加载到tableviewcell中

除了使用if语句之外,是否还有更好的方法将不同行的图像动态加载到tableviewcell中?如果我有很多牢房怎么办。这是否意味着我将不得不编写很多if语句?还有没有办法从核心数据中检索图像并将其用作tableview单元格的图像?提前致谢。
- (UIImage *)imageForMagnitude:(CGFloat)magnitude { 
if (magnitude >= 5.0) {
    return [UIImage imageNamed:@\"5.0.png\"];
}
if (magnitude >= 4.0) {
    return [UIImage imageNamed:@\"4.0.png\"];
}
if (magnitude >= 3.0) {
    return [UIImage imageNamed:@\"3.0.png\"];
}
if (magnitude >= 2.0) {
    return [UIImage imageNamed:@\"2.0.png\"];
}
return nil;
} 编辑:如果它与幅度无关?例如产品列表或类似iPhone上的应用商店。他们如何为桌上的不同应用生成不同的图像?他们使用的是表格还是有更好的方法呢?     
已邀请:
使用
<math.h>
中的
floorf
函数舍入幅度 然后使用
MIN
MAX
宏来保护实际值不超过5或低于2。
int realMagnitude = (int) MAX(MIN(floorf(magnitude), 5.0f), 2.0f);
return [UIImage imageNamed:[NSString stringWithFormat:@\"%d.0.png\", realMagnitude]];
更新:我认为这就是您要寻找的:在
-tableView:cellForRowAtIndexPath:
中,使用提供的
NSIndexPath
来获取相关图像:
cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@\"%d.jpg\", indexPath.row]];
    
嗯,也许是一个转换语句。或者,您也可以将幅度四舍五入,然后..
[NSString stringWithFormat:@\"%d.0.png\", newMagnitude];
    

要回复问题请先登录注册