Ios 添加到tableview单元格的按钮也会出现在其他随机单元格上

Ios 添加到tableview单元格的按钮也会出现在其他随机单元格上,ios,objective-c,uitableview,uibutton,Ios,Objective C,Uitableview,Uibutton,我在向tableview单元格添加按钮时遇到问题。基本上,我是在一个特定的单元格中添加一个按钮,但该按钮也会出现在随机的其他单元格中(当前不在视图中) 点击单元格时,我设置一个变量(tappedCell=indepath),然后重新加载该单元格。在我的cellforrowatinexpath中,我检查indexPath是否等于tappedCell,如果等于,我会向其添加一个按钮。这是可行的,但此按钮也会出现在表视图中较低的其他单元格上。当我点击这些单元格时,它不在视图中,而是在更远的地方,所以我

我在向tableview单元格添加按钮时遇到问题。基本上,我是在一个特定的单元格中添加一个按钮,但该按钮也会出现在随机的其他单元格中(当前不在视图中)

点击单元格时,我设置一个变量(
tappedCell=indepath
),然后重新加载该单元格。在我的
cellforrowatinexpath
中,我检查
indexPath
是否等于
tappedCell
,如果等于,我会向其添加一个按钮。这是可行的,但此按钮也会出现在
表视图中较低的其他单元格上。当我点击这些单元格时,它不在视图中,而是在更远的地方,所以我必须滚动才能看到它们。如果我继续上下滚动(快速),就会出现越来越多带有按钮的单元格。这似乎是完全随机的

我曾尝试在添加按钮的
if语句中添加
NSLog()
,但这不会被多次调用(当我点击原始单元格时)。所以它实际上并没有添加更多的按钮,它必须是在多个单元格中出现的同一个按钮

我不知道为什么会这样。我尝试在每个单元格中添加一个按钮,然后将
hidden=YES
设置为标准,并在
[self.tappedCell:indexPath]
相等时设置
hidden=NO
,但这不会改变任何事情

在这里,您可以看到我用于将按钮添加到我的单元格中的代码:

UIButton *newBtn=[UIButton buttonWithType:UIButtonTypeCustom];
[newBtn setImage:[UIImage imageNamed:@"open.png"] forState:UIControlStateNormal];
[newBtn setFrame:CGRectMake(220, 0, 100, 86)];
[newBtn addTarget:self action:@selector(openImage:) forControlEvents:UIControlEventTouchUpInside];                
[cell.contentView addSubview:newBtn]; 
我不知道我做错了什么,或者我是否做错了什么。值得一提的是,我使用的是标准的Apple/iOS
style=subtitle
cell,而不是定制的。这是为了在我的整个应用程序中保持一致(单元在故事板中制作,我正在重用一个原型单元)

有谁能告诉我为什么它会这样,以及我如何修复它

提前谢谢

编辑1:

这是我的
cellforrowatinexpath
的完整代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Set up the item
    XYZItem *item = [self.itemsInView objectAtIndex:indexPath.row];
    NSString *CellIdentifier = @"itemPrototypeCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    cell.textLabel.text = item.itemName;

    if ([self.selectedItem isEqual:indexPath]) 
    {
        cell.textLabel.numberOfLines = 2;

        UIButton *newBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        [newBtn setImage:[UIImage imageNamed:@"open.png"] forState:UIControlStateNormal];
        [newBtn setFrame:CGRectMake(220, 0, 100, 86)];
        [newBtn addTarget:self action:@selector(openImage:) forControlEvents:UIControlEventTouchUpInside];

        [cell.contentView addSubview:newBtn];
    }

    NSString *detailTextLabelText = [NSString stringWithFormat:@"%lu subitems", (unsigned long)[item.subItems count]];
    cell.detailTextLabel.text = detailTextLabelText;

    return cell;
}

该按钮显示在重复使用先前添加的按钮的单元格上

这里有几个选项

  • 使按钮成为每个单元格的一部分。每次创建/重用属性时,将其设置为
    .hidden
    属性,并将其设置为适当的值
  • 删除单元格的
    prepareforeuse
    方法中的按钮


  • 假设
    self.tappedCell
    是一个
    UITableViewCell
    子类,
    indepath
    是一个索引路径,
    [self.tappedCell isEqual:indepath]
    将始终返回
    NO

    我遇到了同样的问题,而且我还有多个头:

    解决办法是:

        if (!cell)
        {
          cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    
    
            headerLabel = [[UILabel alloc] initWithFrame:
                                    CGRectMake(15, 5, self.tableView.frame.size.width, 15.0)];
    
            headerLabel.backgroundColor = [UIColor clearColor];
            headerLabel.textAlignment = NSTextAlignmentLeft;
            [headerLabel setFont:[UIFont fontWithName:@"Helvetica Neue Medium" size:13.0]];
            headerLabel.tag = 1002;
             [cell.contentView addSubview:headerLabel];
    
            button =  [[UIButton alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, HEIGHTROW)];
            [button addTarget:self action:@selector(eventAddAlert) forControlEvents:UIControlEventTouchUpInside];
            [button setBackgroundImage:[UIImage imageNamed:@"add_manually.png"] forState:UIControlStateNormal];
            button.hidden = YES;
            button.tag = 1001;
    
    
          [cell.contentView addSubview:button];
    
        }else{
    
                // use viewWithTag to find lblNombre in the re-usable cell.contentView
    
             headerLabel = (UILabel *)[cell.contentView viewWithTag:1002];
            button = (UIButton *)[cell.contentView viewWithTag:1001];
    
        }
    
     if(indexPath.section == 0 && indexPath.row == 0){
    
            button.hidden = NO;
    
    
        }else{
           button.hidden = YES;
             .........
    }
    

    您好,谢谢您的快速反馈很抱歉,我理解这种混淆:
    self.tappedCell
    是一个
    indepath
    (被点击的单元格)。所以我不认为这是个问题。现在,你的意思是,按钮显示在以前有按钮的单元格上?还是我误解了?我不明白这是怎么回事,因为问题发生在第一次点击时。即使这是我点击的第一个单元格,按钮仍然会显示在更远的其他单元格上。当我不断上下滚动(快速)时,它也会出现在更多的单元格上(即使我的add button if语句没有运行),我尝试使按钮成为每个单元格的一部分,只需设置
    .hidden
    属性。这没什么区别(但也许我做错了)。我将把
    cellforrowatinexpath
    的全部代码作为编辑添加到我的帖子中,您可以查看一下:)是的。你在重复使用细胞。表视图就是这样工作的。假设您的表视图足够大,可以同时在屏幕上容纳10个单元格。当第一个单元格滚动关闭而第11个单元格滚动打开时,第一个单元格将被重新用作第11个单元格。您注意到的唯一重用是按钮,因为您永远不会删除它。您绝对最好的选择是将
    UITableViewCell
    子类化并使用它。哦,现在我明白了。谢谢你解释tableview是如何工作的,我不知道。。!此外,我将如何做到这一点?我是iOS开发新手,如果这是我应该知道的最基本的东西,我很抱歉。。。但我马上就到了:)