我正在开发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];
}
使用这个UIStackView增加。在UIStackView中,这或多或少是自动的。 – dasdom