开发者

dequeueReusableCellWithIdentifier after resolving memoryleaks

I found a memoryleak using instruments in one of my TableView, exactly at the line:

[[NSBundle mainBundle] loadNibNamed:@"ShopListCell" owner:self options:NULL];

The Identifier from the nib ShopListCell wasn't correct with the CellIdentifier.

Now, I don't have memory-leaks, but my UITabl开发者_运维问答eViewCells have their own-life :-)

I'm using a custom UITableViewCell, and I show some image and update some labels from a NSFetchedResultsController.

When the user clicks in one row, I update the model, so the cell has always the real data to show, but, instead of showing the real data, it shows some other cell.

I suspect this is because I'm reusing cells, but I make all the modifications to the cell before returning it, so I expect to show always the correct data.

This was perfect before fixing the memory-leak, I was using always a new cell, now I'm reusing them but with lots of problems.

The [cell setNeedsDisplay]; before returning the cell has no effect.

Here is some code:

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

static NSString *CellIdentifier = @"ShopListCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

[[NSBundle mainBundle] loadNibNamed:@"ShopListCell" owner:self options:NULL];
cell = nibLoadCell;

cell.backgroundColor = [UIColor clearColor];

}

// Set up the cell...

Ingredient *ingredient = (Ingredient *)[fetchedResultsController objectAtIndexPath:indexPath];

NSLog(@"Section %d Row %d ingredient: %@", indexPath.section, indexPath.row,ingredient.name); // just to be sure it fetchs the correct data, and it does


if([ingredient.isInListDone intValue] == 0) {
cell.accessoryType = UITableViewCellAccessoryNone;
[cellStateButton setSelected:NO];
cellImageInList = nil;

}
else {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[cellStateButton setSelected:YES];
cellImageInList.image = [UIImage imageNamed:@"underlined2.png"];

}

cellLabelName.text = [ingredient name];

 [cell setNeedsDisplay]; // this line has NO effect

return cell;
}

Also I've put a NSLog and it fetches the correct data at correct section and row ...

thanks,

r.


You're creating a cell with

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

and then assigning the cell variable to something else with

cell = nibLoadCell;

The first line essentially has no effect. I would guess that the cell loaded from the nib still does not have its cellIdentifier set correctly. Look here: Loading a Reusable UITableViewCell from a Nib


After reading the other post, finally I've set-up a new class with my custom-cell and now al works as expected, an no memory-leaks!

thanks,

r.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜