Xcode 如果段控制器发生更改,如何删除表视图中的子视图(按钮)?

Xcode 如果段控制器发生更改,如何删除表视图中的子视图(按钮)?,xcode,uitableview,uisegmentedcontrol,Xcode,Uitableview,Uisegmentedcontrol,我面临的问题是,我在UItable视图中对控制器进行了分段,选择索引0,它应该显示按钮(并且工作正常)。问题是,当我选择索引1时,表视图更改了它的内容,这里的按钮都应该被删除。如果我添加这些语句,它只会删除最后一个单元格中的按钮 这些语句只删除最后一个单元格的按钮 调用removefromsuperview时,downloadButton从未添加到视图中。每次使用电池时,它都会重新生成按钮不断堆积 您必须保存旧按钮,而不是创建新按钮并忘记旧按钮:) e、 g: 请关注未来的代码格式化。使阅读变

我面临的问题是,我在UItable视图中对控制器进行了分段,选择索引0,它应该显示按钮(并且工作正常)。问题是,当我选择索引1时,表视图更改了它的内容,这里的按钮都应该被删除。如果我添加这些语句,它只会删除最后一个单元格中的按钮

这些语句只删除最后一个单元格的按钮

调用removefromsuperview时,downloadButton从未添加到视图中。每次使用电池时,它都会重新生成按钮不断堆积

您必须保存旧按钮,而不是创建新按钮并忘记旧按钮:)

e、 g:


请关注未来的代码格式化。使阅读变得非常困难:-)如果我这样做,按钮将显示在两个分段视图的最后一个单元格中。如果您在未显示给我们的代码的某些部分有错误:)。这将正确地在单元格中添加一个按钮。我在UItable视图中对控制器进行了分段,选择索引0,它将显示按钮(并且工作正常)。问题是,当我选择索引1时,表视图更改了它的内容,这里的按钮都应该被删除。如果我添加这些语句,它只会删除最后一个单元格中的按钮[cell willRemoveSubview:downloadButton];[downloadButton removeFromSuperview];downloadButton.hidden=是
        [cell willRemoveSubview:downloadButton];
        [downloadButton removeFromSuperview];
        downloadButton.hidden=YES;
-(void)configureCell: (UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    downloadButton = [UIButton buttonWithType:UIButtonTypeCustom] ;
    [downloadButton setFrame:CGRectMake(250,8,30,30)];
    [downloadButton setTag :indexPath.row];
    [downloadButton setImage:[UIImage imageNamed:@"DownloadLesson.png"] forState:UIControlStateNormal];
    [downloadButton addTarget:self action:@selector(cellButton:) forControlEvents: UIControlEventTouchUpInside];

    if (segOL.selectedSegmentIndex==0) {
        [cell addSubview:downloadButton];
    } else {
        [cell willRemoveSubview:downloadButton];
        [downloadButton removeFromSuperview];
        downloadButton.hidden=YES;
    }
}
- (IBAction)cellButton:(id)sender {

    UIButton *play = sender;   
    NSLog(@"Number of row %d", play.tag]);
}
@interface CellClass : UITableViewCell {
    UIButton *downloadButton;
}

...

-(void)configureCell: (UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    if(!downloadButton) {
        downloadButton = [UIButton buttonWithType:UIButtonTypeCustom] ;
        [downloadButton setFrame:CGRectMake(250,8,30,30)];
        [downloadButton setTag :indexPath.row];
        [downloadButton setImage:[UIImage imageNamed:@"DownloadLesson.png"] forState:UIControlStateNormal];
        [downloadButton addTarget:self action:@selector(cellButton:) forControlEvents: UIControlEventTouchUpInside];
    }        
    if (segOL.selectedSegmentIndex==0) {
        [cell addSubview:downloadButton];
    } else {
        [cell willRemoveSubview:downloadButton];
        [downloadButton removeFromSuperview];
        downloadButton.hidden=YES;
    }
}
- (IBAction)cellButton:(id)sender {

    UIButton *play = sender;

    NSLog(@"Number of row %d", play.tag]);
}