开发者

UITableView crash on scroll

开发者 https://www.devze.com 2023-03-22 08:26 出处:网络
Whenever i scroll my UITableView, it looks at an array to find out what to fill the next cell with, but the array is empty, causing a crash, and appears to have been released somehow. Here is my code:

Whenever i scroll my UITableView, it looks at an array to find out what to fill the next cell with, but the array is empty, causing a crash, and appears to have been released somehow. Here is my code:

.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() stringByAppendingPat开发者_运维问答hComponent:@"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

Any ideas?


My guess is the app crashes when you try to access the value of vaultsPath, which must be deallocated. Because the table view row count is based on the number of elements in the array, the method returning the cells, won't be called if there isn't any element in.
Try to retain the value assigned to vaultsPath, and don't forget to release it later.

0

精彩评论

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

关注公众号