2017-09-11 62 views
0

我正在开发IOS应用程序。当我选择正确创建的didSelectRowAtIndexPath时创建标签和按钮。但是当我点击按钮删除标签和按钮删除完美。但问题是标签的删除标签和按钮后不上移。并更改标签在删除标签后的索引值。请提前帮助感谢。删除按钮上的标签单击和其他标签上移

Code.. 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    y = y + 40; 
    count++; 

    UITableViewCell *selectedCell = [self.tableview cellForRowAtIndexPath:indexPath]; 
    NSLog(@"%@",selectedCell); 

    NSString *str = [array objectAtIndex:indexPath.row]; 

    _label = [[UILabel alloc]initWithFrame:CGRectMake(x,y, 200.0f, 30.0f)]; 
    [_label.layer setBorderColor: [[UIColor blackColor] CGColor]]; 
    [_label.layer setBorderWidth: 1.0]; 
    _label.text = str; 
    _label.tag = indexPath.row; 
    [self.view addSubview:_label]; 

    _button =[[UIButton alloc]initWithFrame:CGRectMake(220.0f,y, 30.0f, 30.0f)]; 
    [_button.layer setBorderColor: [[UIColor blackColor] CGColor]]; 
    [_button.layer setBorderWidth: 1.0]; 
    [_button setTag:indexPath.row]; 
    [_button addTarget:self action:@selector(buttonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside]; 
    [self.view addSubview:_button]; 
} 
- (IBAction)buttonTouchUpInside:(id)sender{ 

    y = y - 40; 

    UIButton *button = (UIButton*)sender; 
    NSInteger index = button.tag; 

    [[sender viewWithTag:index] removeFromSuperview]; 

    _label = (UILabel *)[self.view viewWithTag:index]; 
    [_label removeFromSuperview]; 

    _label =[[UILabel alloc]initWithFrame:CGRectMake(x,y, 200.0f, 30.0f)]; 
    _label.tag = index; 
    [self.view addSubview:_label]; 



    _label = (UILabel *)[self.view viewWithTag:index + 1]; 
    _label.frame = CGRectMake(x,y, 200.0f, 30.0f); 
    _label.tag = index; 
    [self.view addSubview:_label]; 

    _button = (UIButton *)[self.view viewWithTag:index+1]; 
    _button.frame = CGRectMake(220.0f,y, 30.0f, 30.0f); 
    _button.tag = index; 
    _button.backgroundColor =[UIColor blueColor]; 
    [self.view addSubview:_button]; 
} 
+0

使用这个UIStackView增加。在UIStackView中,这或多或少是自动的。 – dasdom

回答

0

我认为你需要在你的方法buttonTouchUpInside

_label =[[UILabel alloc]initWithFrame:CGRectMake(x,y, 200.0f, 30.0f)]; 
_label.tag = index; 
[self.view addSubview:_label]; 

删除此代码,因为你是在同一帧

添加新标签,写这个方法像这样

- (IBAction)buttonTouchUpInside:(id)sender{ 

    y = y - 40; 

    UIButton *button = (UIButton*)sender; 
    NSInteger index = button.tag; 

    [[sender viewWithTag:index] removeFromSuperview]; 

    _label = (UILabel *)[self.view viewWithTag:index]; 
    [_label removeFromSuperview]; 



    _label = (UILabel *)[self.view viewWithTag:index + 1]; 
    _label.frame = CGRectMake(x,y, 200.0f, 30.0f); 
    _label.tag = index; 


    _button = (UIButton *)[self.view viewWithTag:index+1]; 
    _button.frame = CGRectMake(220.0f,y, 30.0f, 30.0f); 
    _button.tag = index; 
    _button.backgroundColor =[UIColor blueColor]; 

} 

不需要此声明

[self.view addSubview:_label]; 

[self.view addSubview:_button]; 

,一旦你在你看来

+0

这不起作用。 –

+0

你有试过更新的答案吗? –

+0

是的,我试过了ans –