2011-07-04 11 views
0

ナビゲーションコントロールのテーブルビューにあるディレクトリにファイルをリストしたいとします。 ディレクトリ内のファイルを表示します。 しかし、私が下にスクロールすると、シミュレータがクラッシュします。何が問題ですか? dirArrayは.hファイルでNSArray * dirArrayとして定義されています。contentsOfDirectoryAtPathがクラッシュする

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    NSString *path = [[NSFileManager defaultManager]currentDirectoryPath]; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    dirArray = [fileManager contentsOfDirectoryAtPath:path error:nil];  
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section { 
    return [dirArray 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] autorelease]; 
    } 
    NSString *fileName = [dirArray objectAtIndex:indexPath.row]; 
    cell.textLabel.text = fileName; 

    return cell; 
} 
+0

がクラッシュログを与えます。 – Ishu

答えて

2

あなたの問題は

dirArray = [fileManager contentsOfDirectoryAtPath:path error:nil]; 

は、自動解放オブジェクトにdirArrayポインティングを持っているということです。したがって、後でそれにアクセスするときには、割り当てが解除されています。それを保持する。

dirArray = [[fileManager contentsOfDirectoryAtPath:path error:nil] retain]; 

あるいははるかに優れ、プロパティとして宣言し、

self.dirArray = [fileManager contentsOfDirectoryAtPath:path error:nil]; 
+0

ありがとうございます。できます。 – user698200

0

あなたは、配列のプロパティを作成し、それを@synthesize必要があります。

または

あなたは、配列をALLOCとのdeallocでそれをrelese SHOLD。

+0

私は\tを追加しました。dirArray = [[NSArray alloc] init]; contentsOfDirectoryAtPathを呼び出す前に、私はdeallocでそれをリリースしました。それはまだクラッシュします。内部でのみ使用されていても財産を作り合成する必要がありますか? – user698200

+0

何がcrashlogですか? –

+0

2011-07-04 16:06:33.032 NaviTest [1211:207] - [NSCFString objectAtIndex:]:インスタンスに送信された認識できないセレクタ0x4e1d090 2011-07-04 16:06:33.035 NaviTest [1211:207] *** 'NSCFString objectAtIndex:]:インスタンスに送信された認識できないセレクタ0x4e1d090' 'NSException'のインスタンスをスローした後に終了します プログラム受信信号: "SIGABRT"。 (gdb) – user698200

関連する問題