2012-09-29 3 views
1

Documents Directory의 데이터를 표시하는 UITableView를 설정하려고합니다.Documents Directory의 파일을 UITableView-iOS5에 표시하십시오.

이 나는 ​​스토리 보드없이 내 응용 프로그램을 만드는 오전

나는 등 구글과 포럼에서 많은 예를 시도로 모든 코드 그래서 조금, 코드에 대한 손실입니다.

위젯 및 DataView가 표시되도록 UITableView가 표시되어 있습니다. 콘텐츠가 필요합니다.

나는 당신을 표시하려면이 코드를 가지고 있지만, 그것은 어떤 데이터가 표시되지 않습니다

- (void)viewDidLoad 
    { 
    [super viewDidLoad]; 

    _firstViewWithOutNavBar = [[UIView alloc] init]; 
    _firstViewWithOutNavBar.frame = CGRectMake(self.view.frame.origin.x, 0, self.v iew.frame.size.width, self.view.frame.size.height); 
    _firstViewWithOutNavBar.backgroundColor = [UIColor whiteColor]; 
    [self.view addSubview:_firstViewWithOutNavBar]; 

    UITableView *tableView = [[UITableView alloc] init]; 
    tableView.frame = CGRectMake(self.view.frame.origin.x, 0,  self.view.frame.size.width, self.view.frame.size.height); 
    tableView.delegate = self; 
    tableView.dataSource = self; 
    [_firstViewWithOutNavBar addSubview:tableView]; 
    } 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
    //alloc and init view with custom init method that accepts NSString* argument 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:indexPath.row]; 
    NSString*pathToPass = [documentsDirectory stringByAppendingPathComponent: 
         [tableView cellForRowAtIndexPath:indexPath].textLabel.text]; //pass this. 

    NSLog(@"%@", pathToPass); 

//_nsarray = [[NSArray alloc] initWithContentsOfFile:pathToPass]; 


    } 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    { 
     return [_nsarray count]; 
     NSLog(@"%@", _nsarray); 
    } 

- (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]; 
     } 

     cell.textLabel.text = [NSString stringWithFormat:@"%@",[_nsarray objectAtIndex:indexPath.row]]; 

     return cell; 
     } 

어떤 도움이 좋을 것.

답변

1

numberOfRowsInSectioncellForRowAtIndexPath의 테이블 뷰 데이터 원본으로 사용되는 배열 _nsarray은 코드에서 초기화되지 않습니다. 당신은 viewDidLoad

NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 
_nsarray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:docPath error:NULL]; 

같은 것을해야한다.

+0

슈퍼 - 코드가 작동하여 매우 멋지게 만들었습니다 :-) –

1

numberOfRowsInSection 메서드에 중단 점을 배치하여이 메서드가 호출되었는지 여부를 확인 했습니까? 코드에서 볼 수 있듯이 _nsarray를 초기화하지 않았고 해당 배열에 객체를 추가하지 않았습니다. 기본적으로 배열에는 0 개의 개체가 포함되어 있으므로 행을 만들지 않습니다. numberOfRowsInSection 메소드에서 return 문 다음에 nslog를 두었습니다. 실제로는 배열 값을 볼 수 있도록 return 문 앞에 배치하십시오. 도움이되기를 바랍니다.

+0

감사의 말 :-) 도움을 주셨습니다. –

관련 문제