繁体   English   中英

iOS - EXC_BAD_ACCESS 错误

[英]iOS - EXC_BAD_ACCESS Error

我正在开发 iPhone/iPod 应用程序。 下面的代码是一个UIViewController的.m文件。我得到如下:

Thread 1: EXC_BAD_ACCESS (code=2......

当我点击以下行时:

cell.textLabel.text = [datasource objectAtIndex:indexPath.row];

我知道这通常发生在您释放后尝试访问 object 时,但我不会在尝试访问它之前释放它。 我在下面附上了完整的代码。

感谢任何帮助!

#import "HomePage.h"
#import "HusbandryRecordsMain.h"
#import "TaskManagerMain.h"
#import "AnimalInventoryMain.h"
#import "FeedInventoryMain.h"

@implementation HomePage
@synthesize options, datasource;

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad {
    [self setupArray];
    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.
}

-(void)setupArray{
    options = [NSMutableArray  arrayWithObjects:@"Husbandry Records", @"Task Manager", @"Feeder Inventory", @"Animal Inventory", nil];

    datasource = options;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return 4;
}

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

    UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];    
    }

    [[cell textLabel] setBackgroundColor:[UIColor clearColor]];
    [[cell detailTextLabel] setBackgroundColor:[UIColor clearColor]];

    // THE FOLLOWING LINE IS THROWING THE ERROR!
    cell.textLabel.text = [datasource objectAtIndex:indexPath.row]; 

    //Arrow 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0){
        HusbandryRecordsMain *hrm = [self.storyboard instantiateViewControllerWithIdentifier:@"Husbandry Records - Main"];
        [self.navigationController pushViewController:hrm animated:YES];
    }
    else if (indexPath.row == 1){
        TaskManagerMain *tmm = [self.storyboard instantiateViewControllerWithIdentifier:@"Task Manager - Main"];
        [self.navigationController pushViewController:tmm animated:YES];
    } 
    else if (indexPath.row == 2){
        FeedInventoryMain *fim = [self.storyboard instantiateViewControllerWithIdentifier:@"Feeder Inventory - Main"];
        [self.navigationController pushViewController:fim animated:YES];
    }  
    else if (indexPath.row == 3){
        AnimalInventoryMain *aim = [self.storyboard instantiateViewControllerWithIdentifier:@"Animal Inventory - Main"];
        [self.navigationController pushViewController:aim animated:YES];
    }

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

//----------------------TABLEVIEWCELL HEIGHT -------------------------------------------

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 70;

}

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

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

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

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

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

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end
-(void)setupArray{
    options = [NSMutableArray  arrayWithObjects:@"Husbandry Records", @"Task Manager", @"Feeder Inventory", @"Animal Inventory", nil];

    datasource = options;
}

您正在将自动释放的 object 直接分配给datasource实例变量,然后在崩溃的代码行中释放后尝试使用它。 如果你打开僵尸检测,它很可能会直接捕捉到它。 同样,static 分析器(构建和分析)应该已经捕获了它。

(当然,除非你启用了 ARC,此时其他事情正在发生......)

您的文件选项和数据源是什么类型? 也可以尝试 self.options 等。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM