Ios UITableViewCell中的UIButton未从其superview单元格中删除

Ios UITableViewCell中的UIButton未从其superview单元格中删除,ios,iphone,Ios,Iphone,我已经试了很久了 (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (ce

我已经试了很久了

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

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell  = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ;
}
long row = [indexPath row];
cell.textLabel.text = items[row];
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.highlightedTextColor = [UIColor blackColor];
cell.detailTextLabel.text = alertsTimeArray[row];
cell.textLabel.frame =  CGRectMake(0, 0, 30, 10);
cell.detailTextLabel.textColor = [UIColor whiteColor];
cell.detailTextLabel.highlightedTextColor = [UIColor blackColor];

NSLog(@">>  %@",alertsTypeArray);
    if (![[alertsTypeArray objectAtIndex:indexPath.row] isEqualToString:@"Critical"]){

    NSLog(@">>  %@",alertsTypeArray);
    UIButton *    reset = [UIButton buttonWithType:UIButtonTypeCustom];
    [reset addTarget:self
              action:@selector(customActionPressedForAlertView:)
    forControlEvents:UIControlEventTouchUpInside];
    [reset setTitle:@"RESET" forState:UIControlStateNormal];
    [reset setTitleColor:[UIColor colorWithRed:0/255.0 green:126/255.0 blue:255/255.0 alpha:1.0] forState:UIControlStateNormal];
    reset.frame = CGRectMake(250.0f, 22.0f, 70.0f, 30.0f);
    reset.tag = indexPath.row;
    [cell addSubview:reset];

   }


cell.backgroundColor = [UIColor clearColor];
return cell;
}
但我无法从超级视图单元中移除重置按钮,请帮助我

每次我的警报,Pearray都会填写两个不同的警报:严重警报和服务警报。如果是服务,则我需要添加重置按钮,否则我不应该添加。如果添加了重置按钮,并且用户单击了“重新发送”按钮,则行应该删除,因此对于我删除了alertsTypeArray类型中的一个,但重置按钮没有从选项卡中删除。如果我加载表视图,则重置按钮会出现严重警报


谢谢,

如rdelmar所述,您很可能正在删除该按钮,但也可能在同一单元格中添加了多个按钮,因为您添加时没有先检查按钮是否存在

只需检查按钮是否已经存在,如果不存在,只需将其添加到单元格中

给你的按钮一个像123这样的任意标记,并在添加之前检查该标记是否存在于单元格中

因此,改变:

 if (![[alertsTypeArray objectAtIndex:indexPath.row] isEqualToString:@"Critical"]){

    NSLog(@">>  %@",alertsTypeArray);
    UIButton *    reset = [UIButton buttonWithType:UIButtonTypeCustom];
    [reset addTarget:self
              action:@selector(customActionPressedForAlertView:)
    forControlEvents:UIControlEventTouchUpInside];
    [reset setTitle:@"RESET" forState:UIControlStateNormal];
    [reset setTitleColor:[UIColor colorWithRed:0/255.0 green:126/255.0 blue:255/255.0 alpha:1.0] forState:UIControlStateNormal];
    reset.frame = CGRectMake(250.0f, 22.0f, 70.0f, 30.0f);
    reset.tag = indexPath.row;
    [cell addSubview:reset];

   }
致:

卸下按钮应该比现在简单一点

(void)customActionPressedForAlertView:(UIButton *)sender
{
    UIButton *pressedButton = sender;
    [pressedButton removeFromSuperview];
}

你想做什么还不清楚。请解释你的程序应该如何工作。@CaptJak,为什么这么严重?如果你的按钮的动作方法被调用,没有理由removeFromSuperview不工作。要么该方法没有被调用,要么您的单元格中有多个按钮在彼此的顶部,而您只是删除了顶部的按钮。当重复使用已有按钮的单元格时,您在cellForRow中的代码将添加另一个按钮。@CaptJak每次我的警报Pearray将填充两个不同的警报临界和服务如果是服务我需要添加重置按钮,否则我不应该,如果添加了“重置”按钮,并且用户单击了“重新发送”按钮,则该行应被删除,因此对于我删除的alertsTypeArray类型之一,但是对于选项卡,我没有删除“重置”按钮,如果我加载表视图,则“重置”按钮出现严重警报。@rdelmar那么我应该如何解决此问题,
    if (![[alertsTypeArray objectAtIndex:indexPath.row] isEqualToString:@"Critical"])
{
    NSLog(@">>  %@",alertsTypeArray);
    UIButton *reset = (UIButton *)[cell.contentView viewWithTag:123];

    if (reset == nil)
    {
        UIButton * reset = [UIButton buttonWithType:UIButtonTypeCustom];
        [reset addTarget:self action:@selector(customActionPressedForAlertView:) forControlEvents:UIControlEventTouchUpInside];
        [reset setTitle:@"RESET" forState:UIControlStateNormal];
        [reset setTitleColor:[UIColor colorWithRed:0/255.0 green:126/255.0 blue:255/255.0 alpha:1.0] forState:UIControlStateNormal];
        reset.frame = CGRectMake(250.0f, 22.0f, 70.0f, 30.0f);
        reset.tag = 123;
        [cell.contentView addSubview:reset];
   }
(void)customActionPressedForAlertView:(UIButton *)sender
{
    UIButton *pressedButton = sender;
    [pressedButton removeFromSuperview];
}