iOS - UITableViewCell 初始化失败,EXC_BAD_ACCESS

发布于 2024-12-06 12:20:47 字数 1953 浏览 4 评论 0原文

我有一个简单的视图,它是 UINavigationController 的第三级,它显示笔尖中的一个空表,这是 .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 的第二级有相同的代码,它工作正常,真的不明白出了什么问题。

感谢您的帮助:)

i've a simple View that is the third level of a UINavigationController, it show an empty table from a nib, here is the code of the .m file:

#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

When i run it, the device crash and the debugger say that there is a EXC_BAD_ACCESS at this line:

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

i've the same code in the second level of the UINavigationController and it work fine, realy don't understand what's wrong.

Thanks for any help :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你爱我像她 2024-12-13 12:20:47
  static NSString *CellIdentifier =@"Cell";

不再

  static NSString *CellIdentifier = "Cell";

结束

 return cell;//not found on ur code
  static NSString *CellIdentifier =@"Cell";

not

  static NSString *CellIdentifier = "Cell";

more over

 return cell;//not found on ur code
掩于岁月 2024-12-13 12:20:47

也许您应该在此方法中返回单元格

- (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时添加@

May be you should return cell in this method:

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

Updated:

And as mentioned @AppleVijay add @ when initializing CellIdentifier

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文