我正在开发IOS App。选择正确创建的didSelectRowAtIndexPath时创建Label和Button。但当我点击按钮删除标签和按钮删除完美。但问题是标签在删除标签和按钮后没有向上移动。并在删除标签后更改标签的索引值。请提前帮助谢谢。
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 :(得分: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];
在视图中添加后