Search code examples
iphoneobjective-cjailbreakuitableview

UITableView Crashes when scrolling and [reloadData]


I'm creating a app using theos (jailbreak) and I have created a UITableView in the loadView method in my RootViewController.mm file:

- (void)loadView {
    self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease];
    self.view.backgroundColor = [UIColor redColor];



    NSError * error;
    NSArray * directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:@"/var/mobile/Library/BannerImage" error:&error];

    countries = [NSDictionary dictionaryWithObject:directoryContents forKey:@"Themes"];
   mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStyleGrouped];

    [mainTableView setDataSource:self];
    [mainTableView setDelegate:self];
    [self.view addSubview:mainTableView];
}

And the rest of my code:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [countries count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [[countries allKeys] objectAtIndex:section];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *continent = [self tableView:tableView titleForHeaderInSection:section];
    return [[countries valueForKey:continent] 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];
    }
        // Configure the cell...

    NSString *continent = [self tableView:tableView titleForHeaderInSection:indexPath.section];
    NSString *country = [[countries valueForKey:continent] objectAtIndex:indexPath.row];

    cell.textLabel.text = country;

    cell.accessoryType = UITableViewCellAccessoryNone;
    if([self.checkedIndexPath isEqual:indexPath])
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{


    UITableViewCell *object = [tableView cellForRowAtIndexPath:indexPath];
    [object setSelected:NO animated:YES];
    NSString *selectedTheme = [[object textLabel] text];

    NSDictionary *plistFile = [NSDictionary dictionaryWithObject:selectedTheme forKey:@"CurrentTheme"];
    [plistFile writeToFile:@"/Applications/BannerImage.app/CurrentTheme.plist" atomically:YES];

    if(self.checkedIndexPath)
    {
        UITableViewCell* uncheckCell = [tableView
                                        cellForRowAtIndexPath:checkedIndexPath];
        uncheckCell.accessoryType = UITableViewCellAccessoryNone;
    }
    UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    self.checkedIndexPath = indexPath;
}

It crashes when I scroll up and try to scroll back down and it crashes when [mainTableView reloadData] is called. How can I fix this? Crash Log


Solution

  • I found out that I needed to retain the Dictionary because it was being released. So instead of

    NSDictionary *countries;
    

    change to this

    @property(nonatomic, retain) NSDictionary *countries;
    

    and

    @synthesize countries;
    

    in your .mm file and use

    self.countries = [NSDictionary dictionaryWithObject:directoryContents forKey:@"Themes"];
    

    instead of

    countries = [NSDictionary dictionaryWithObject:directoryContents forKey:@"Themes"];