-1

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

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

        [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]);
}
4

1 回答 1

0

当您调用 removefromsuperview 时,downloadButton 永远不会添加到视图中。每次使用电池时都会重新制作。按钮不断堆积

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

例如:

@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]);
}
于 2012-12-09T10:43:36.787 回答