Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
IPHONE:手机上的UITableView按钮报告错误标签_Iphone_Iphone Sdk 3.0 - Fatal编程技术网

IPHONE:手机上的UITableView按钮报告错误标签

IPHONE:手机上的UITableView按钮报告错误标签,iphone,iphone-sdk-3.0,Iphone,Iphone Sdk 3.0,我有一个包含图像和按钮的表格。每个按钮都有一个带有不同编号的标签。单击时,所有按钮执行相同的方法。我将在按钮运行的方法上使用按钮的标记,以了解单击了哪个按钮 问题是按钮的标签被报告是错误的。我想,当这些细胞被重复使用时,有什么东西干扰了标签 这是我用来动态填充表的代码: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static

我有一个包含图像和按钮的表格。每个按钮都有一个带有不同编号的标签。单击时,所有按钮执行相同的方法。我将在按钮运行的方法上使用按钮的标记,以了解单击了哪个按钮

问题是按钮的标签被报告是错误的。我想,当这些细胞被重复使用时,有什么东西干扰了标签

这是我用来动态填充表的代码:

  - (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;
}

这就是buyNow方法

- (void) buyNow:(id)sender {

    int index = [sender tag];

    NSLog(@"button clicked = %d", index);
}
被报告为单击按钮的按钮从0循环到6,从未报告超过6的数字。我认为这与被重复使用的细胞相对应,为什么数量没有变化,这是一个谜

如何解决这个问题


感谢您的帮助。

尝试移动创建和设置按钮的代码,并将其添加到单元格中的
willDisplayCell
方法:

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

我认为这将允许您重用单元格,并且每个单元格中都有一个独特的按钮实例。

如果您不重用单元格,它是否有效?