EXC_BAD_ACCESS due to PostNotification

I ran into one problem regarding one module so that I could clear the thread for the same one.

I have one configured UITableviewCell.

When I receive new information, I send one notification

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

In the field of view where I maintain the table, I launch the customized cell

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

now at customcell.mm

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

and at dealloc

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

Now my application crashes due to this notification and dealloc is never called.

Can you guys help me how to get this job or something that I am doing wrong here?

Thank,

Sagar

+3
source share
1 answer

Methods initWithFrame:reuseIdentifier:and deallocare incomplete. Is it on purpose?

initWithFrame:reuseIdentifier: should contain a call to super:

- (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];
}
+6

Source: https://habr.com/ru/post/1750075/


All Articles