2011-07-24 177 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的值,並且不要稍後忘記發佈它。