tags:

views:

53

answers:

3

Hello All,

I am facing one issue regarding one module let me clear the flow for the same.

I have one customized UITableviewCell.

When I am getting some new information I am posting one notification

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

In view where I am maintaining the table I am initiating a customized cell

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

now in customcell.mm

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

and in dealloc

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

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

Can you guys help me, how to get this working or anything I m doing wrong over here...

Thanks,

Sagar

A: 

Can you check the GotSomething: of the customcell, is it here? Is the method signature correct?

vodkhang
A: 

can you please type the declaration and definition of "GotSomething" method here....

Sanniv
It seems issue is with UITableViewCell releasing. as in my case dealloc never get called and because of which Notification fails as it refers invalid things.. I am releasing the table but this customized cell are not getting released.
Sagar Mane
I see, some how the retain count has increased of your customized cell, you need to write a clean up method on your own and call your self for every instance you are preparing inside the customized cell class.
Sanniv
+1  A: 

You initWithFrame:reuseIdentifier: and dealloc methods are incomplete. Is it on purpose ?

initWithFrame:reuseIdentifier: should contains 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;
}

and dealloc too:

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

Update

The cell is not auto-release after its creation. So the cell is leaking and never gets deallocated. The code should be:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell= [[CustomCell alloc] initWithFrame: reuseIdentifier:identifier document:doc];
    return [cell autorelease];
}
Laurent Etiemble
Hi Laurent Thanks for reply.. These two i.e init and dealloc are with same prototype as you have said.. Issue is dealloc for customcell never get called.. the retain count for this cell remain 3. and as dealloc not get called my notification is not removed properly. Though we release table which is using custom cell,I am not able to understand how to release these custom cell.
Sagar Mane
In the "tableView: cellForRowAtIndexPath:" method, you are creating a cell and return it without auto-releasing it. This may be why the cell never gets deallocated.
Laurent Etiemble
I tried with this approach.. but not working....
Sagar Mane
Why don't you use the "dequeueReusableCellWithIdentifier:" method in the "tableView:cellForRowAtIndexPath:" method ? It is the recommended way to optimize cell creation and management (See http://developer.apple.com/iphone/library/documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html).
Laurent Etiemble
Thanks Laurent for this reference and will update my code with these concepts..
Sagar Mane