我有一个示例代码,可以从下面的代码中显示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 :(得分: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
}
答案 1 :(得分:2)
替换它:
cell.imageView.image = [UIImage imageNamed:@"cell-image.png"];
使用:
cell.imageView.image = (UIImage *)[_finaImages objectAtIndex:index];
答案 2 :(得分: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];
}