2009-11-16 4 views
1

TableView에서 Ringtones 디렉터리의 내용을 나열하려고하지만 셀당 파일 대신 모든 셀의 디렉터리에서 마지막 파일 만 가져옵니다. 그것은 잘로드목록에있는 UITableView 목록 내용

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    Profile_ManagerAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
     cell.hidesAccessoryWhenEditing = YES; 
    } 

    cell.accessoryType = UITableViewCellAccessoryNone; 
    //cell.textLabel.text = @"No Ringtones"; 
    //cell.textLabel.text = @"Test"; 

    NSString *theFiles; 
    NSFileManager *manager = [NSFileManager defaultManager]; 
    NSArray *fileList = [manager directoryContentsAtPath:@"/Test"]; 
    for (NSString *s in fileList){ 
     theFiles = s; 
    } 
    cell.textLabel.text = theFiles; 

    return cell; 
} 

내가 NSLog를 사용하는 경우, 오류가,이 디렉토리에있는 모든 파일을 잘 나열하지 : 이것은 내 코드입니다. 심지어 [s objectAtIndex:indexPath.row] 시도했지만 objectAtIndex : 오류가 발생합니다. 누구든지 아이디어가 있습니까?

+0

주 파일의 아무 곳에서나 파일에 액세스 할 수 있도록 머리글에 배열을 만들어야합니다. 그렇게하지 않으면 숫자를 사전 설정하지 않는 한 어떻게 테이블 항목 수를 설정할 수 있습니까? 그러나 사람들이 파일을 추가하거나 삭제하면 디렉토리의 항목 수를 얻는 것이 더 쉬워집니다. – Maximilian

답변

0

for 루프는 파일을 반복하고 파일을 현재 경로로 설정합니다. 따라서 루프의 마지막 부분에서 theFiles는 컬렉션의 마지막 문자열이됩니다.

같은 것을보십시오 :

cell.textLabel.text = [fileList objectAtIndex:indexPath.row]; 
+0

당신은이 대답에 반 정답입니다. 실제로 틀린 것은 NSMutableArray가 objectAtIndex를 허용 한 것입니다. – WrightsCS

+0

objectAtIndex는 NSArray에서 제공되지만 그렇지 않습니까? –

1

실제로 내가 내 자신의 질문에 대답, 10 분 미만, 여기 원인에 대한 질문을 사랑 해요!

이 내가 일을 위의 코드를 가지고하는 방법입니다

NSMutableArray *theFiles; 
NSFileManager *manager = [NSFileManager defaultManager]; 
NSArray *fileList = [manager directoryContentsAtPath:@"/Test"]; 
for (NSString *s in fileList){ 
    theFiles = fileList; 
} 
cell.textLabel.text = [theFiles objectAtIndex:indexPath.row]; 
return cell; 

은 그냥있는 NSString있는 NSMutableArray을했고, 그 날이 objectAtIndex를 사용하는 것을 허용했다. 이제 파일 확장자를 자르십시오!

+0

여기서는 배열을 여러 번 복사한다는 것을 알 수 있습니다. 이는 매우 비효율적입니다. for 루프는 필요 없으며 배열의 가변 복사본을 만들어야 할 필요가 없다는 것을 알고 있습니다. –

1

당신은있는 NSString, NSMutableArray를하고 루프를 제거해야합니다 .. 최종 코드는 다음과 같아야합니다

NSFileManager *manager = [NSFileManager defaultManager]; 
NSArray *fileList = [manager directoryContentsAtPath:@"/Test"]; 
cell.textLabel.text = [fileList objectAtIndex:indexPath.row]; 
return cell; 

BTW이의 fileList 및 관리자는 .. 각 셀에 대해 반복적으로 생성 그래서 그것을 확인하는 것이 좋습니다 UITableViewController의 전역 변수 인 1을 할당하십시오.

관련 문제