2013-03-25 38 views
2

我終於嘗試從NSMutableArray中的表視圖單元格圖像視圖中加載圖像。但我得到警告 由於未捕獲異常'NSInvalidArgumentException'而終止應用程序,原因:' - [UIImage length]:無法識別的選擇器已發送到實例。由於未捕獲的異常'NSInvalidArgumentException',原因:' - [UIImage length]:無法識別的選擇器發送到實例

我嘗試了很多解決方案張貼在這個網站。但它不適合我。

這是我的代碼:

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]]; 
+0

將nil設置爲recipe1.numbers1 = [[NSMutableArray alloc] initWithObjects:[UIImage imageNamed:@「1.png」],...,nil]; – NANNAV 2013-03-25 07:29:01

回答

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]; 
+0

即使在更改我的代碼後,我也會得到同樣的警告 – haritha 2013-03-25 07:35:31

+0

我編輯了我的答案,希望它有幫助 – tkanzakic 2013-03-25 07:41:03

+0

對不起,我找不到我和代碼之間的任何區別..這是我從中發出警告的同一個警告。 – haritha 2013-03-25 07:46:06

0

你做錯了變化這條線:

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

TO

cell1.numbersImageview1.image = [recipe1.numbers1 objectAtIndex:indexPath.row]; 
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]; 
} 
相關問題