2013-03-22 100 views
0

我有一個樣本代碼在從下面的代碼在GridView來顯示圖像...但它爲所有的圖像顯示相同的圖像...該方法是以下..陣列圖像的顯示

- (MMGridViewCell *)gridView:(MMGridView *)gridView cellAtIndex:(NSUInteger)index 
{ 
MMGridViewDefaultCell *cell = [[MMGridViewDefaultCell alloc] initWithFrame:CGRectNull]; 
cell.textLabel.text = [NSString stringWithFormat:@"Cell %d", index]; 
    cell.imageView.image = [UIImage imageNamed:@"cell-image.png"]; 
// this needs to be change load from the array 
} 


- (NSInteger)numberOfCellsInGridView:(MMGridView *)gridView 
{ 
return 64; 
} 

現在我有圖像的陣列,需要投入的是cellAtIndex方法,但我應該怎麼做才能轉化爲對於所有圖像放入數組..

數組就像

-(void)viewDidLoad{ 
_imageDatas = [DelegateClass mag]._imageData; 
_finaImages = [[NSMutableArray alloc]init]; 
} 
_finaImages = [[NSMutableArray alloc]init]; 
for (int i = 0; i < [_imageDatas count]; i++) { 

    [_finaImages addObject:[self loadImage:i]]; 
} 

- (UIImage *)loadImage:(int)index { 
@try { 
    UIImage* image = [[UIImage alloc] initWithData:[_imageDatas objectAtIndex:index]] ; 

    return image; 
} 
@catch (NSException *exception) { 
    NSLog(@"ImageFullScreenViewController - loadImage Exception Name = %@ Exception Reason = %@",[exception name],[exception reason]); 
    } 

如何從下面的陣列從數據到上述imageView.image

+0

讓我指出在第四行有一個額外的}你的viewDidLoad,它應該使它無法編譯。 – WolfLink 2013-03-22 05:40:30

回答

2

試試這個

- (MMGridViewCell *)gridView:(MMGridView *)gridView cellAtIndex:(NSUInteger)index 
{ 
MMGridViewDefaultCell *cell = [[MMGridViewDefaultCell alloc] initWithFrame:CGRectNull]; 
cell.textLabel.text = [NSString stringWithFormat:@"Cell %d", index]; 
    cell.imageView.image =[imagesArray objectAtIndex: index]; 
// this needs to be change load from the array 
} 
+0

- [ArtOfSleeping _isResizable]:無法識別的選擇器發送到實例0x88fb960 2013-03-22 15:14:32.859雜誌[2822:17c03] ***由於未捕獲的異常'NSInvalidArgumentException',原因:' - [ArtOfSleeping _isResizable ]:無法識別的選擇器發送到實例0x88fb960' – user2102546 2013-03-22 09:46:45

+0

確保imagesArray具有UIImage對象..如果您正在放置ArtOfSleeping對象的對象,那麼它可能會導致該問題。 – BhushanVU 2013-03-22 09:50:05

1
- (MMGridViewCell *)gridView:(MMGridView *)gridView cellAtIndex:(NSUInteger)index 
{ 
    MMGridViewDefaultCell *cell = [[MMGridViewDefaultCell alloc] initWithFrame:CGRectNull]; 
    cell.textLabel.text = [NSString stringWithFormat:@"Cell %d", index]; 
    // Use this 
    cell.imageView.image = [self loadImage:index]; 
} 
2

替換此:

cell.imageView.image = [UIImage imageNamed:@"cell-image.png"]; 

有了:

cell.imageView.image = (UIImage *)[_finaImages objectAtIndex:index]; 
+0

- [ArtOfSleeping _isResizable]:無法識別的選擇器發送到實例0x88fb960 2013-03-22 15:14:32.859雜誌[2822:17c03] ***由於未捕獲的異常'NSInvalidArgumentException',原因:' - [ArtOfSleeping _isResizable ]:無法識別的選擇器發送到實例0x88fb960' – user2102546 2013-03-22 09:46:02

+0

請'NSLog(@「%@',_ finalImages);'如果你的上面的代碼是正確的,那麼它將工作 – 2013-03-22 09:48:35

+0

事情是圖像在url中並從web服務從imageDatas數組,它顯示捕捉錯誤,當我試圖運行代碼 – user2102546 2013-03-22 10:29:15