ttotto
ttotto

Reputation: 837

UITableView reloadData crash

I added CustomImageView to the UITableViewCell.

    UITableViewCell *cell = nil;
    if (cell == nil) {
         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:@"Cell1"] autorelease];
    }
    CustomImageView *customIV = [[CustomImageView alloc] init];
    [cell.contentView addSubView:customIV];
    [customIV release];

But when I try to reload tableview, the error occurs.

error call stack is same as follows.

enter image description here

Output string is as follows.

-[CustomImageView superview]: message sent to deallocated instance 0x1f848f30

Upvotes: 3

Views: 1215

Answers (4)

rmaddy
rmaddy

Reputation: 318955

You only want to add the image once to each cell. Change your code to this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell1"];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:@"Cell1"] autorelease];
        CustomImageView *customIV = [[CustomImageView alloc] init];
        [cell.contentView addSubView:customIV];
        [customIV release];
    }

    return cell;
}

If this doesn't work then you need to show your complete cellForRowAtIndexPath method. By only showing part of your code you make it difficult to help.

Upvotes: 1

iPatel
iPatel

Reputation: 47119

This error occur because each time CustomImageView object created when your cell created.

so, best way is first initialize object of CustomImageView then create your UITableView

such like,

CustomImageView *customIV put it in your .h file and then @synthesize it in .m File

(put this code above UITableView)

self.customIV = [[CustomImageView alloc] init];

Then create UITableView

self.tablView = [[UITableView alloc]init];
.
.
.
.

And in cellForRowAtIndexPath only add [cell.contentView addSubView:self.customIV];

Upvotes: 0

Niru Mukund Shah
Niru Mukund Shah

Reputation: 4733

Try to Comment this line[customIV release]; & run , it should not crash while reloading data.

The reason behind this is everytime it tries to create new custom view & releases it, so causes extra load on system & crash occurs.

Upvotes: 1

Manann Sseth
Manann Sseth

Reputation: 2745

CustomImageView *customIV = [[CustomImageView alloc] initWithFrame:CGRectMake(x, y, w, h)];
[cell.contentView addSubView:customIV];

It's done with me when I've released memory.
So according to me, No need to release, because it'll deallocated memory.

Hopefully, it'll help you.
Thanks.

Upvotes: 1

Related Questions