【问题标题】:Can’t display images in a table view无法在表格视图中显示图像
【发布时间】:2012-04-02 12:45:18
【问题描述】:

我正在尝试通过代码为表格视图中的单元格设置图像。我在我的主包(Øvelser/Pictures)中的一个文件夹的子目录中拥有所有图片,并且从 NSLog() 可以看到数组中的所有图片,但应用程序不会获取它们。下面的代码和崩溃报告:

 @implementation STATableViewController
 @synthesize nameOfExercises = _nameOfExercises;
 @synthesize exercisePicture = _exercisePicture;

 - (void)viewDidLoad
 {
[super viewDidLoad];    

//will get the name of the files in the exercise folder
_nameOfExercises = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"Øvelser/Text"]error: nil];  

 _exercisePicture = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"Øvelser/Pictures"]error: nil]; 

NSLog(@"%@", _nameOfExercises);
NSLog(@"%@", _exercisePicture);

 }     

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
return [_nameOfExercises count];
 }

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
   cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//the extension is removed, before it's set to the labeltext
NSString *cellValue = [[_nameOfExercises objectAtIndex:indexPath.row] stringByDeletingPathExtension];
cell.textLabel.text = cellValue;
cell.imageView.image = [_exercisePicture objectAtIndex:indexPath.row]; 


return cell;
 }

控制台:

2012-04-02 14:31:58.324 STA[9665:11f03] (
"Fisketelefon.txt",
"Krammebamse.txt",
"Raketstart.txt"
)

2012-04-02 14:31:58.326 STA[9665:11f03] (
"Fisketelefon.jpg",
"Krammebamse.jpg",
"Raketstart.jpg"
)
2012-04-02 14:31:58.329 STA[9665:11f03] -[__NSCFString _isResizable]: unrecognized selector sent to instance 0x785d5e0
2012-04-02 14:31:58.330 STA[9665:11f03] *** Terminating app due to uncaught exception     'NSInvalidArgumentException', reason: '-[__NSCFString _isResizable]: unrecognized selector sent to instance 0x785d5e0'
*** First throw call stack:
(0x14b7052 0x1a6bd0a 0x14b8ced 0x141df00 0x141dce2 0x4ed91b 0x3290 0x49de0f 0x49e589 0x489dfd 0x498851     0x443301 0x14b8e72 0xe792d 0xf1827 0x77fa7 0x79ea6 0x10530c 0x4034c6 0x403bd6 0x412743 0x4131f8 0x406aa9 0x23a4fa9 0x148b1c5 0x13f0022 0x13ee90a 0x13eddb4 0x13edccb 0x4032a7 0x404a9b 0x23f8 0x2355)
terminate called throwing an exceptionwarning: Attempting to create USE_BLOCK_IN_FRAME variable with block     that isn't in the frame.
warning: Attempting to create USE_BLOCK_IN_FRAME variable with block that isn't in the frame.
warning: Attempting to create USE_BLOCK_IN_FRAME variable with block that isn't in the frame.
warning: Attempting to create USE_BLOCK_IN_FRAME variable with block that isn't in the frame.

【问题讨论】:

    标签: objective-c ios cocoa-touch uitableview uiimage


    【解决方案1】:

    您应该将图像(UIImage 类的实例)分配给imageView.image 属性。但是你在那里存储的是保存图像的文件的名称。您必须将文件名转换为实际图像:

    NSString *pathPrefix = [[NSBundle mainBundle] bundlePath]
        stringByAppendingPathComponent:@"Øvelser/Pictures"];
    NSString *imageName = [_exercisePicture objectAtIndex:indexPath.row];
    NSString *fullImagePath = [pathPrefix stringByAppendingPathComponent:imageName];
    [[cell imageView] setImage:[UIImage imageWithContentsOfFile:fullImagePath]];
    

    【讨论】:

    • 应用程序不再崩溃,但仍然不会显示图片。我知道我在 _exercisePictures 中引用的文件夹确实包含图片,但我是否只获得这些图片的名称?
    • 啊,是的,你必须将整个路径传递给initWithContentsOfFile:初始化器,否则它将无法加载图像。
    • 我不确定你的意思是什么?
    • 我已经更新了代码示例,它应该可以工作并且让事情变得清晰。
    【解决方案2】:

    一个疯狂的猜测:

    _exercisePicture 中的对象是字符串。然后您尝试将图像设置为字符串:

    cell.imageView.image = [_exercisePicture objectAtIndex:indexPath.row];
    

    请确保_exercisePicture 中的对象是图像。

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多