0

我正在制作一个 UITableView,每个单元格都有一个 UIIMage。为了为每个单元格设置不同的图像,我在项目中加载了所有图像,并创建了一个包含所有图像的数组_flagsArray = [NSArray arrayWithObjects: @"flag_Italia.png", @"flag_USA", nil];

cellForRowAtIndexPath我写的

UIImageView * flagImageView = (UIImageView *) [self.view viewWithTag:1]; //declaration of UIImageView in cell
UIImage * flagImage = (UIImage *)[_flagsArray objectAtIndex:indexPath.row]; //read UImage
[flagImageView setImage : flagImage]; //should assign UImage to UIImageView

运行它时 log 崩溃,*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString size]: unrecognized selector sent to instance 0xcbdc'第三行有信号 SIGABRT 但我不明白为什么,怎么了?

谢谢!

4

1 回答 1

0

您的数组包含字符串而不是图像。此外,看起来您正试图从self.view应该从cell. 这样做:

UIImageView *flagImageView = (UIImageView *)[cell viewWithTag:1];
NSString *imageName = _flagsArray[indexPath.row];
UIImage *flagImage = [UIImage imageNamed:imageName];
flagImageView.image = flagImage;

或者更简洁地说:

UIImageView *flagImageView = (UIImageView *)[cell viewWithTag:1];
flagImageView.image = [UIImage imageNamed:_flagsArray[indexPath.row]];
于 2013-08-11T20:04:48.337 回答