views:

514

answers:

2

I have a table containing images and buttons. Each button has a tag with a different number. All buttons execute the same method when clicked. I will use the button's tag on the method they run, to know which button was clicked.

The problem is that the button's tag being reported is wrong. I imagine that as the cells are being reused, something is interfering with the tag.

This is the code I am using to populate the table on the fly:

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                   reuseIdentifier:MyIdentifier] autorelease];

    UIButton *buyButton = [[UIButton alloc] initWithFrame:CGRectMake( 220, 4, 100, 35)];

    buyButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    buyButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [buyButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    buyButton.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    [buyButton setBackgroundImage:newImage forState:UIControlStateNormal];
    [buyButton addTarget:self action:@selector(buyNow:) forControlEvents:UIControlEventTouchDown];
    [buyButton setTag: 1]; // I have to do this, to locate the button a few lines below
    [cell addSubview:buyButton];
    [buyButton release];
}


 imageU = [[[UIImage alloc]initWithContentsOfFile:[[NSBundle mainBundle] 
                              pathForResource:[NSString stringWithFormat: @"table-pg%d",numberX] 
                                                    ofType:@"jpg"]] autorelease];
    cell.imageView.image = imageU;

    UIButton * myButton = (UIButton *) [cell viewWithTag:1];  
    [myButton setTitle:NSLocalizedString(@"buyKey", @"") forState:UIControlStateNormal]; 

    UIImage *newImage = [[[[UIImage alloc]initWithContentsOfFile:[[NSBundle mainBundle] 
                                             pathForResource: @"whiteButton" ofType:@"png"]] autorelease]
                                                     stretchableImageWithLeftCapWidth:12.0f topCapHeight:0.0f];

    [buyButton setTag:indexPath.row]; // the button's tag is set here

return cell;

}

and this is the buyNow method...

- (void) buyNow:(id)sender {

    int index = [sender tag];

    NSLog(@"button clicked = %d", index);
}

the button being reported as the clicked one cycles from 0 to 6, no number beyond 6 is ever reported. I think this corresponds to the cells being reused, why the number is not changing, is a mystery.

How to solve that?

thanks for any help.

+2  A: 

Try moving the code that creates and sets up the button and adds it to the cell to the willDisplayCell method:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

I think this will allow you to reuse cells and also have a unique instance of the button in each one.

No Surprises
thanks! that's it! You are D man!
Digital Robot
A: 

Yep "No Surprises"'s answer works very well. I had exactly the same problem as Mike and came to the conclusion that something was messing with the index path in redrawing.

The following worked for me just fine. Thanks No Surprises for a good post.

-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{

switch (indexPath.section) {
    case 0:
        cell.tag = 100;
        break;
    default:
        cell.tag = indexPath.row;
        [(PlayerCell*)cell cardButton1].tag = 2*indexPath.row;
        [(PlayerCell*)cell cardButton2].tag = 2*indexPath.row+1;
        break;
}

}

SpecialK