简体   繁体   中英

IPHONE: UITableView button on cell reporting wrong tag

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM