开发者

contentsOfDirectoryAtPath crashes

I want to list files in the directory in table view of nav开发者_StackOverflowigation control. It shows files in the directory. But whenever I scroll down, my simulator crashes. What is the problem? dirArray is defined in .h file as NSArray *dirArray.

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


Your problem is that

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

will have dirArray pointing to an autoreleased object. So by the time you are accessing it later, it is being deallocated. Either retain it.

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

Or much better, declare it as a property and do

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


you should make property of the your array and @synthesize it.

or

you shold alloc array and relese it in the dealloc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜