iOS - UITableViewCell初始化失败,EXC_BAD_ACCESS

时间:2011-09-23 10:02:52

标签: ios uitableview exc-bad-access

我有一个简单的View,它是UINavigationController的第三级,它从一个nib显示一个空表,这里是.m文件的代码:

#import "ThirdLevel.h"


@implementation ThirdLevel

@synthesize lista, categoria;


- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"Categoria: %@", categoria);

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return NO;
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // 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;
}

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 20;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    //newtableView.separatorColor = [UIColor clearColor];
    static NSString *CellIdentifier = "Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

}

- (void)buttonPressed:(id)sender {
    NSLog(@"premuto");

}


- (void)dealloc {
    [super dealloc];
}


@end

当我运行它时,设备崩溃,调试器说这行有一个EXC_BAD_ACCESS:

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

我在UINavigationController的第二级中使用相同的代码并且它工作正常,真的不明白什么是错的。

感谢您的帮助:)

2 个答案:

答案 0 :(得分:4)

  static NSString *CellIdentifier =@"Cell";

  static NSString *CellIdentifier = "Cell";

更多

 return cell;//not found on ur code

答案 1 :(得分:1)

这个方法可能是return cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    //newtableView.separatorColor = [UIColor clearColor];
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    return cell;
}

<强>更新

如前所述@AppleVijay在初始化@时添加CellIdentifier