我面临的问题是我在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]);
}
答案 0 :(得分:0)
downloadButton永远不会添加到视图中。每次使用细胞时都会重新制作。 按钮不断堆积
你必须保存旧按钮而不是新按钮而忘记旧按钮:)
e.g:
@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]);
}