因未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [UIImage length]:无法识别的选择器发送到实例

时间:2013-03-25 07:19:01

标签: ios cocoa-touch uiimageview

我尝试从NSMutableArray中加载表格视图单元格图像视图中的图像。但我得到警告 由于未捕获的异常'NSInvalidArgumentException'而终止应用,原因:' - [UIImage长度]:无法识别的选择器发送到实例。

我在这个网站上发布了很多解决方案。但它不适合我。

这是我的代码:

recipe1.numbers1=[[NSMutableArray alloc] initWithObjects:
                                         [UIImage imageNamed:@"1.png"], 
                                         [UIImage imageNamed:@"2.png"], 
                                         [UIImage imageNamed:@"3.png"], 
                                         [UIImage imageNamed:@"4.png"], 
                                         [UIImage imageNamed:@"5.png"], 
                                         [UIImage imageNamed:@"6.png"], 
                                         [UIImage imageNamed:@"7.png"], 
                                         [UIImage imageNamed:@"8.png"],                                                                                                                                                                                     
                                         [UIImage imageNamed:@"9.png"], 
                                         [UIImage imageNamed:@"10.png"], nil];

这是我访问这些图片的方式:

cell1.numbersImageview1.image = [UIImage imageNamed:[recipe.numbers1 objectAtIndex:indexPath.row]];

3 个答案:

答案 0 :(得分:2)

错误是您从阵列访问图像的方式,您已经在那里存储了UIImage对象,没有必要(并且是一个错误)来调用imageNamed:,更改行到:

cell1.numbersImageview1.image = [recipe.numbers1 objectAtIndex:indexPath.row];

修改

在您的代码中,您将对数组元素的访问权添加为另一个对象,请尝试将整个代码更改为:

recipe1.numbers1=[[NSMutableArray alloc] initWithObjects:
                   [UIImage imageNamed:@"1.png"], [UIImage imageNamed:@"2.png"], 
                   [UIImage imageNamed:@"3.png"], [UIImage imageNamed:@"4.png"], 
                   [UIImage imageNamed:@"5.png"], [UIImage imageNamed:@"6.png"], 
                   [UIImage imageNamed:@"7.png"], [UIImage imageNamed:@"8.png"], 
                   [UIImage imageNamed:@"9.png"], [UIImage imageNamed:@"10.png"], nil];


cell1.numbersImageview1.image = [recipe.numbers1 objectAtIndex:indexPath.row];

答案 1 :(得分:0)

你做错了改变这一行:

cell1.numbersImageview1.image = [UIImage imageNamed:[recipe.numbers1 objectAtIndex:indexPath.row]];nil];

cell1.numbersImageview1.image = [recipe1.numbers1 objectAtIndex:indexPath.row];

答案 2 :(得分:0)

you try this one also once.

- (void)viewDidLoad
{
   recipe1.numbers1=[[NSMutableArray alloc] initWithObjects:@"1.png",@"2.png",@"3.png",@"4.png",@"5.png",@"6.png",@"7.png",@"8.png",@"9.png",@"10.png"];
}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
      ....................
      ....................

NSString *imagename=[recipe1.numbers1 objectAtIndexPath:indexPath.row];
cell1.numbersImageview1.image =[UIImage imageNamed:imagename];
}