尝试设置图像的NSCFConstantString大小

时间:2013-08-11 13:17:47

标签: uitableview uiimageview uiimage nscfstring

我正在为每个单元格创建一个UIIMage的UITableView。 要为每个单元格设置不同的图像,我在项目中加载了所有图像,并创建了一个包含所有图像_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

运行它与日志*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString size]: unrecognized selector sent to instance 0xcbdc'崩溃,信号SIGABRT在第三行,但我不明白为什么,出了什么问题?

谢谢!

1 个答案:

答案 0 :(得分: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]];