2010-06-15 85 views
3

我面临一个关于一个模块的问题,让我清除相同的流程。由于PostNotification导致的EXC_BAD_ACCESS

我有一个自定义的UITableviewCell。

当我得到我张贴一个通知

[[NSNotificationCenter defaultCenter] postNotificationName:KGotSomething object:nil userInfo:message]; 

鉴于我在哪里维护表我发起定制的电池

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    cell= [[CustomCell alloc] initWithFrame: reuseIdentifier:identifier document:doc]; 
    return cell; 
} 

现在customcell.mm

一些新的信息
- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier 
{ 
[[NSNotificationCenter defaultCenter] addObserver:self 
       selector:@selector(GotSomething:) 
         name:KGotSomething 
         object:nil]; 
} 

and dealloc

- (void)dealloc 
{ 
    [[NSNotificationCenter defaultCenter] removeObserver:self 
        name:KGotSomething 
         object:nil]; 
} 

现在我的应用程序崩溃,由于此通知和dealloc永远不会被调用。

你们能帮助我,怎么得到这个工作什么的我米做错了在这里...

感谢,

萨加尔

+0

你可以检查GotSomething:的customcell,是否在这里?方法签名是否正确? – vodkhang 2010-06-15 09:14:49

回答

6

initWithFrame:reuseIdentifier:dealloc方法是不完整的。这是故意的吗?

initWithFrame:reuseIdentifier:应该包含超的电话:

- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     [[NSNotificationCenter defaultCenter] addObserver:self 
       selector:@selector(GotSomething:) 
         name:KGotSomething 
         object:nil]; 
    } 
    return self; 
} 

dealloc太:

- (void)dealloc 
{ 
    [[NSNotificationCenter defaultCenter] removeObserver:self 
        name:KGotSomething 
         object:nil]; 
    [super dealloc]; 
} 

更新

细胞是不会自动释放它的创作之后。所以这个单元正在泄漏并且永远不会被释放。代码应该是:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    cell= [[CustomCell alloc] initWithFrame: reuseIdentifier:identifier document:doc]; 
    return [cell autorelease]; 
} 
+0

嗨,Laurent感谢您的回复..这两个即init和dealloc与您所说的具有相同的原型。问题是dealloc for customcell永远不会被调用..该单元的保留计数保持为3.并且由于dealloc没有被调用我的通知未被正确删除。虽然我们发布了使用自定义单元格的表格,但我无法理解如何释放这些自定义单元格。 – 2010-06-16 17:38:31

+0

在“tableView:cellForRowAtIndexPath:”方法中,您正在创建一个单元格并将其返回而不会自动释放它。这可能是为什么细胞永远不会被释放。 – 2010-06-16 18:08:31

+0

我尝试过这种方法..但不工作.... – 2010-06-18 12:26:28

相关问题