2012-04-02 23 views
1

Ich versuche, das Bild für die Zelle in einer Tabellenansicht nach Code festzulegen. Ich habe alle Bilder in einem Unterverzeichnis eines Ordners in meinem Mainbundle (Øvelser/Pictures) und vom NSLog() kann man alle Bilder im Array sehen, aber die App bekommt sie nicht. Code und Absturzbericht unter:Kann Bilder in einer Tabellenansicht nicht anzeigen

@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; 
} 

Konsole:

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. 

Antwort

2

Sie sollen ein Bild (eine Instanz der Klasse UIImage) in die imageView.image Eigenschaft zuweisen. Aber was Sie dort speichern, ist ein Name der Datei, die das Bild enthält. Sie müssen den Dateinamen in ein tatsächliches Bild umwandeln:

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]]; 
+0

Die App stürzt nicht mehr ab, aber es zeigt immer noch nicht die Bilder. Ich weiß, dass der Ordner, den ich in den _exercisePictures verweise, zwar Bilder enthält, aber bekomme ich nur den Namen dieser Bilder? –

+0

Ah ja, Sie müssen den gesamten Pfad zum 'initWithContentsOfFile:' Initialisierer übergeben, sonst kann das Bild nicht geladen werden. – zoul

+0

Ich bin mir nicht sicher, was du damit meinst? –

1

Eine wilde Vermutung:

Die Objekte in _exercisePicture sind Strings. Und dann versuchen Sie ein Bild auf die Zeichenfolge zu setzen:

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

Bitte stellen Sie sicher, dass die Objekte in _exercisePicture Bilder sind.

Hoffe, es hilft

Verwandte Themen