UITutViewCell中的UIButton未从其超级视图单元格中删除

时间:2015-02-04 15:48:40

标签: ios iphone

我很长时间以来都在尝试这项任务

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

(void)customActionPressedForAlertView:(id)sender{

UIButton *mybutton =  (UIButton *)sender;
[mybutton removeFromSuperview];

UIAlertView *resetAlert = [[UIAlertView alloc] initWithTitle:@"Are you sure to reset alert?" message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
[resetAlert show];
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
selectedIndex =  indexPath.row;
}

但是我无法从超级查看单元中删除RESET按钮请帮助我。

每次我的alertsTypeArray都会填写两个不同的警报,"严重"和"服务"。如果是服务,那么我需要添加重置按钮,否则我不应该,如果添加了重置按钮并且用户点击了重新发送按钮,那么行应该删除,所以对于我删除了其中一个alertsTypeArray类型,但是重置按钮没有从表格中删除,如果我加载表格视图,重置按钮会进入严重警报。

谢谢,

1 个答案:

答案 0 :(得分:0)

正如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];

   }

要:

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