2011-07-24 8 views
1

私は自分のUITableViewをスクロールするたびに、次のセルを埋めるために配列を探しますが、配列は空であり、クラッシュの原因となり、何とか解放されたようです。ここに私のコードは次のとおりです。スクロールでUITableViewがクラッシュする

の.h

@interface HomeViewController : UITableViewController { 

    NSArray *vaults; 

} 


@property (retain) NSArray *vaults; 

@end 

.M

#import "HomeViewController.h" 

NSString *vaultsPath; 

@implementation HomeViewController 

@synthesize vaults; 

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 

    vaultsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Vaults"]; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    self.vaults = [fileManager contentsOfDirectoryAtPath:vaultsPath error:nil]; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [self.vaults 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 *dictionaryPath = [NSString stringWithFormat:@"%@/%@", 
           vaultsPath, 
           [self.vaults objectAtIndex:indexPath.row]]; //Crashes at this line, with the self.vaults array now empty. 
    NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:dictionaryPath]; 
    cell = [AHCellCreation createCellWithDictionary:dictionary Cell:cell]; 

    return cell; 
} 


- (void)dealloc 
{ 
    [super dealloc]; 
    [self.vaults release]; 
} 

@end 

任意のアイデア?

答えて

4

vaultsPathの値にアクセスしようとするとアプリがクラッシュすると思われますが、これは割り当てを解除する必要があります。テーブルビューの行数は配列内の要素数に基づいているため、要素がない場合はセルを返すメソッドは呼び出されません。
vaultsPathに割り当てられた値を保持しようとし、ドンしません後でそれを解放することを忘れないでください。