tags:

views:

206

answers:

4

Hi, i have one button with image for selection in UITableviewCell.i have added button action as toggleButton method.if i touch button in particular Cell, the image of Button of Corresponding tablecell is changed.but i when i scroll tableview the changed image is in another cell.how can i avoid it?please see my code….will you tell what i have to do..?i don't want to use did select method in which i have to do other Operation.any help please?

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

static NSString *CellIdentifier = @"Cell";

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

    onButton = [UIButton buttonWithType:UIButtonTypeCustom];
 onButton.tag = 1;  

 onButtonView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 50)];
 onButtonView.tag = 2;
 onButtonView.image = [UIImage imageNamed:@"NotSelected.png"];
 [onButton setBackgroundImage:[onButtonView.image stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0] forState:UIControlStateNormal];
 [cell addSubview:onButton];
 [onButton addTarget:self action:@selector(toggleButton:) forControlEvents: UIControlEventTouchUpInside];
 [onButtonView release];
 } 


return cell;

  }
A: 

When you reuse cell in your -cellForRowAtIndexPath method you must reset all its properties, or the cell may have the properties you've set to it previously for another indexPath.
So you must save cell states somewhere and in -cellForRowAtIndexPath set an image for your onButton each time method gets called.

Vladimir
really, number of questions with the same problem is awful)
Morion
Just because you guys have seen this question many times doesn't mean that the questioner has. The insults are really unnecessary.
Kristopher Johnson
Agree, sorry did not intended my answer to be offensive. I'm not a native speaker - may not feel some language details. Sorry again
Vladimir
Vladimirwill u give example pls?
Mikhail Naimy
+2  A: 

You get this problem because you are re-using the cells shown. This is the right way to create your cell because otherwise you'll use to much memory.

First of all, remove the following from the if-case. Put it right beneath:

onButton = [UIButton buttonWithType:UIButtonTypeCustom];
onButton.tag = 1;               

onButtonView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 50)];
onButtonView.tag = 2;
onButtonView.image = [UIImage imageNamed:@"NotSelected.png"];
[onButton setBackgroundImage:[onButtonView.image stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0] forState:UIControlStateNormal];
[cell addSubview:onButton];
[onButton addTarget:self action:@selector(toggleButton:) forControlEvents: UIControlEventTouchUpInside];
[onButtonView release];

What you are doing is you are trying to get a cell by "dequeueReusableCellWithIdentifier". This means that if the cell does not exist yet, (if (cell == nil)) it will create a cell. You are only setting the button image when the cell should be created. If you set it after the if-case, you will alwais set the image to "not selected" even if your cell issn't nil.

Start off whith that, it might fix your problem.

Best regards, Paul Peelen

Paul Peelen
I believe that this will add a new instance of a UIButton each time the cell is being scrolled into view. This might not be visible, but will slow down the scroll performance due to the many views within the cell.
Felix
i did as u told....problem is ?for example images are changed when i touch first cell's button. when i scroll tableview,the first cell's button's image is in last cells button(changed).... any help please?
Mikhail Naimy
images of button os changed ...but it is not saved in tableviewcell..however when i scroll tableview ,it disappears
Mikhail Naimy
I guess that you mean that the cell gets reset? On the top of my head... What you can try is to create a NSMutableArray and add every button change to it. When loading the cell check if the button exists (ex. by using the indexPath.row), if not... show the unchecked image, if so... show the checked image.Anyone with an better idea, welcome to answer :)
Paul Peelen
+1  A: 

Your tableView:cellForRowAtIndexPath: method will return previously used cells that are no longer in use. You need to ensure that if you are reusing a cell, you reset the image. You need to do something like this:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell) {
    // Reusing cell; make sure it has correct background
    UIImageView *onBut = (UIButton *)[cell viewWithTag:1];
    onBut.image = [UIImage imageNamed:@"NotSelected.png"];
    // etc.
}
else {
    // Create cell
    // ...
}

Note that if your selected cell gets scrolled back into view, you'll need to set the image to the "selected" image.

Kristopher Johnson
A: 

If you have limited table size, easiest way to create NSArray with pre-initialized cells. Memory usage is not essential for dozen of cells

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [myCells objectAtIndex:indexPath.row];
}
vaddieg