2012-04-02 8 views
1

코드별로 테이블 뷰에서 셀의 이미지를 설정하려고합니다. 내 메인 번들 (Ø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. 

답변

2

을 도움이되기를 바랍니다. 그러나 거기에 저장하고있는 것은 이미지를 보유하고있는 파일의 이름입니다. 파일 이름을 실제 이미지로 바꿔야합니다.

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

앱이 더 이상 충돌하지 않지만 사진은 계속 표시되지 않습니다. _exercisePictures에있는 참조보다 폴더에 그림이 포함되어 있지만 그 그림의 이름 만 가져올 수 있습니다. –

+0

아 네, 전체 경로를'initWithContentsOfFile :'이니셜 라이저에 전달해야합니다. 그렇지 않으면 이미지를로드 할 수 없습니다. – zoul

+0

나는 그게 무슨 뜻인지 모르겠다. –

1

야생 추측 : _exercisePicture에서

개체는 문자열입니다. 그리고 당신은 문자열에 이미지를 설정하려고 :

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

_exercisePicture의 객체 이미지 있는지 확인하십시오.

는 당신은 imageView.image 특성에 이미지합니다 (UIImage 클래스의 인스턴스)를 할당하기로되어있다

관련 문제