【问题标题】:EXC_BAD_ACCESS crash when switching back and forth between views在视图之间来回切换时 EXC_BAD_ACCESS 崩溃
【发布时间】:2011-11-29 06:11:08
【问题描述】:

在视图之间来回切换时出现 EXC_BAD_ACCESS 崩溃。我在查找这次崩溃的原因时遇到了问题。在模拟器中,它总是返回到 main.m 文件并在其中报告崩溃。

但是在我的设备上,当我在 dealloc 方法中释放它时,EXC_BAD_ACCESS 会显示在我的自定义 UITableViewCell 上。如果我启用 NSZombieEnabled,我的应用根本不会崩溃。

这是.h文件

#import <UIKit/UIKit.h>
#define kWinsAmountTagValue 2 // how many wins you have
#define kWinningsAmountTagValue 3 // how much money you won

@interface MyStatsViewController : UIViewController 
<UITableViewDelegate, UITableViewDataSource,
UINavigationBarDelegate, UINavigationControllerDelegate>{
    NSArray *list;
    UITableView *theTable;
    UITableViewCell *theCell;
}

@property (nonatomic, retain) NSArray *list;
@property (nonatomic, retain) IBOutlet UITableView *theTable;
@property (nonatomic, retain) IBOutlet UITableViewCell *theCell;

// dealloc and cleanup
-(void) dealloc;

// misc methods
-(void)loadData;

// demo data
-(NSArray *)tableData;

@end

这是我的 .m 文件

#import "MyStatsViewController.h"

@implementation MyStatsViewController
@synthesize list;
@synthesize theTable;
@synthesize theCell;

#pragma mark - dealloc and cleanup
- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    NSLog(@"Memory Warning");
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.list = nil;
    self.theTable = nil;
    self.theCell = nil;
}

- (void)dealloc
{
    [super dealloc];
    [list release];
    [theTable release];
    [theCell release];
}

#pragma mark - misc methods
-(void) loadData
{
    self.list = [self tableData];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

-(void)viewWillAppear:(BOOL)animated
{
    [self loadData];
    [theTable reloadData];
}

#pragma mark - Table Data Source Methods
-(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
    return [list count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView
        cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier =@"MyStatsCustomCellIdentifer";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
    NSUInteger row = [indexPath row];

    if (cell == nil) {
        if (row == [list count] -1) {
            cell = [[[UITableViewCell alloc]
                     initWithStyle:UITableViewCellStyleDefault
                     reuseIdentifier:CellIdentifier] autorelease];
        } else {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyStatsCustomCell"
                                                         owner:self
                                                       options:nil];

            if ([nib count] > 0) {
                cell = self.theCell;
            } else {
                NSLog(@"failed to load MyStatsCustomCell");
            }
        }
    }
    // Add custom stuff here for rows    
    //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    if (row == [list count] -1) {
        cell.textLabel.text = [list objectAtIndex:row];
    } else {
        UILabel *prizeLevel = (UILabel *)[cell viewWithTag:kPrizeLevelTagValue];
        prizeLevel.text = [[list objectAtIndex:row] objectForKey:@"prizeLevel"];

        UILabel *winsAmount = (UILabel *)[cell viewWithTag:kWinsAmountTagValue];
        winsAmount.text = [[list objectAtIndex:row] objectForKey:@"winsAmount"];

        UILabel *winningsAmount = (UILabel *)[cell viewWithTag:kWinningsAmountTagValue];
        winningsAmount.text = [[list objectAtIndex:row] objectForKey:@"winningsAmount"];
    }
    //NSLog(@"theCell Retain: %i",[theCell retainCount]);
    return cell;
}

#pragma mark - Table View Delegate Methods
-(void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
} 

#pragma mark - demo data
-(NSArray *)tableData
{
    NSArray *prizeLevels = [[NSArray alloc] initWithObjects: 
                            @"6-of-6", @"5-of-6", @"4-of-6",@"3-of-6", nil];

    NSArray *winsAmount = [[NSArray alloc] initWithObjects: 
                            @"0", @"0", @"2", @"100", nil];

    NSArray *winngingsAmount = [[NSArray alloc] initWithObjects: 
                            @"$0",@"$0", @"$45.50",@"$125.00", nil];

     NSMutableArray *myGames = [[[NSMutableArray alloc] init] autorelease];

    for (int i = 0; i < [prizeLevels count]; i++) {
        NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];

        [dict setObject:[prizeLevels objectAtIndex:i] forKey:@"prizeLevel"];
        [dict setObject:[winsAmount objectAtIndex:i] forKey:@"winsAmount"];
        [dict setObject:[winngingsAmount objectAtIndex:i] forKey:@"winningsAmount"];

        [myGames addObject:dict];
        [dict release];
    }
    [prizeLevels release];
    [winsAmount release];
    [winngingsAmount release];

    [myGames addObject:@"Spent: $1250.00"];
    return myGames;
}

@end

任何帮助将不胜感激。

【问题讨论】:

  • @Guven 感谢您的帮助。这似乎解决了我的问题。我不敢相信这么简单的事情让我这么头疼。
  • 完美,很高兴听到您的问题已解决。我也会将其添加为答案,以便您可以将问题的状态更新为“已回答”(通过选择正确答案)。这样,其他有相同问题的人也将被引导到答案。

标签: ios uitableview exc-bad-access


【解决方案1】:

在调用 super 的析构函数之前清理类自己的变量是一个好习惯。可以在此处找到更多详细信息:Why do I have to call super -dealloc last, and not first?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多