views:

25

answers:

2

I have the following problem:

I have a UITableView with some Text and some Images as content. The TextLabel of the cell displays the text and I add a UIView with some UIImageViews as its subview to the cells contentview.

Everything works fine until I delete some cells. What happens is, I delete a cell (lets say the first) from the table, reload its data and e.g. then second cell moves one further up BUT! the UIView to the right contains the content from the first (deleted) cell.

I dont know why this happens - though I assume somethings wrong in me cellForRowAtIndexPath callback method.

I'll paste some code to make it clearer.

What I can do, is unload the controller and load it again - then the images contain the correct content again....

Heres my code:

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



static NSString *MyIdentifierUNCHECK = @"MyIdentifierUNCHECK";


MyStuff *m = [allObjects objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:MyIdentifierUNCHECK];
    UIView *v;    
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifierUNCHECK] autorelease];
        v = [[[UIView alloc] initWithFrame:CGRectMake(5.0, 5.0, 70.0, 70.0)] autorelease];

        v.contentMode = UIViewContentModeScaleAspectFill;
        [v addSubview:[[UIImageView alloc] init]];//some image view
        [cell.contentView addSubview:v];
    }

    cell.textLabel.text = m.name;
    cell.textLabel.numberOfLines = 0;
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica-BoldOblique" size:14.0];
    cell.imageView.image = [UIImage imageNamed:@"checkbox.png"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;
A: 

I (luckily :)) figured it out - I should've taken the view, which was existent already, from my cells content..... and exchange its content. Thats why I tagged it in first place.... this helped me out:

    v = [cell.contentView viewWithTag:PRICE_TAG];
    for (UIView *t in v.subviews) {
        [t removeFromSuperview];
    }
    [v addSubview:[self setupPrice:m.price]];

Should read more documentation....

Icky
+1  A: 

I am assuming by "view in the right" you mean the view that u instanciate there for your cell...The problem here occurs because you reuse cells and are not resetting the UIView for each cell, you will see your same problem occur if you have enough cells that you can scroll through them. When you reuse cells you must always assume they have dirty data and should reload them with the right data, in your case, your mistake is in this snippet:

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifierUNCHECK] autorelease];
    v = [[[UIView alloc] initWithFrame:CGRectMake(5.0, 5.0, 70.0, 70.0)] autorelease];

    v.contentMode = UIViewContentModeScaleAspectFill;
    [v addSubview:[[UIImageView alloc] init]];//some image view
    [cell.contentView addSubview:v];
}

You are only instantiating the view in the cell when its first allocated, so this is whats happening... 1- You Make all your cells initially and make their "right view" 2- You delete a cell 3- For your second cell, the system is reusing the UITableViewCell that you created as your first cell 4- Since the cell isnt nil the UIView isnt being set and what you see is the first cells UIView for the second cell

Since you are not resetting the cells UIView, you are seeing the wrong view in the cell instead of the one you expect.

In order to fix this you can either choose not to reuse cells OR you can move the code that adds the UIView to occur everytime cellForRowAtIndexPath is called, that way the right UIView will be loaded with the right cell

Daniel
correct! thanks for reformulating that, i really should read the api more thorough....
Icky