这就是我的想法。有一个TableView
。每个单元格都有一个按钮。当我点击它时,该按钮被删除,而ProgressView
则被子视图。问题是,当我没有滚动表时,一切似乎都工作正常。但是在滚动时,一切都搞砸了,按钮和ProgressView
的命令混在一起。
以下是代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Download Button
DownloadButtonCell=[[UIButton alloc]initWithFrame:CGRectMake(10, 30, 48, 48)];
[DownloadButtonCell setImage:[UIImage imageNamed:@"download.png"] forState:UIControlStateNormal];
DownloadButtonCell.tag=111;
[DownloadButtonCell addTarget:self action:@selector(startDownload:)
forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:DownloadButtonCell];
return cell;
}
这是下载方法,删除下载按钮并显示进度视图:
-(void)startDownload:(id)sender
{
CGPoint buttonPosition=[sender convertPoint:CGPointZero toView:self.tableview];
NSIndexPath *indexPath = [self.tableview indexPathForRowAtPoint:buttonPosition];
NSLog(@"%ld",(long)indexPath.row);
LLACircularProgressView *progressView=[[LLACircularProgressView alloc]initWithFrame:CGRectMake(20, 40, 25, 25)];
progressView.tintColor = [UIColor greenColor];
[progressView addTarget:self action:@selector(stopDownload:) forControlEvents:UIControlEventTouchUpInside];
[[[tableview cellForRowAtIndexPath:indexPath] viewWithTag:111]removeFromSuperview];
[[[tableview cellForRowAtIndexPath:indexPath]contentView] addSubview:progressView];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[mp3Array objectAtIndex:indexPath.row]]];
operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
NSString *pdfName = [[idArray objectAtIndex:indexPath.row] stringByAppendingString:@".mp3"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:pdfName];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Successfully downloaded file to %@", path);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
progressView.progress=(float)totalBytesRead / totalBytesExpectedToRead;
}];
[operation start];
}
答案 0 :(得分:0)
您应该查看覆盖tableviewcell子类中的prepareForReuse方法,并确保单元格以一致的状态开始。
看起来你总是在cellForRowAtIndexpath方法中添加下载按钮,这意味着当单元格被重用时,总是会添加下载按钮,这意味着每个单元格最终可能会添加多个下载按钮...当你删除按钮以添加进度条,在这种情况下你不清楚要删除哪个按钮(这在苹果文档中含糊不清)。
看起来如果已经有一个进度视图添加到即将重用的单元格中,它将不会被删除,所以在cellForRowAtIndexPath之后你会在进度视图的顶部有一个下载按钮:
//DownloadableTableViewCell.h
@interface DownloadableTableViewCell : UITableViewCell
@property (assign, nonatomic, getter=isDownloading) BOOL downloading;
@end
//DownloadabletableViewCell.m
@interface DownloadableTableViewCell()
@property (strong, nonatomic) UIView *downloadButton;
@property (strong, nonatomic) UIView *progressView;
@end
@implementation
-(id)initWithStyeleBlahblah{
self = [super initWithStyleBlahBlah];
if (self){
//init download button, init progress view.
//add download button to view.
}
return self;
}
-(void)setDownloading:(BOOL)downloading{
_downloading = downloading;
if (_downloading){
//add progress view to contentview.
//remove download button.
}
else{
//add button to contentView;
//remove progress view;
}
}
-(void)prepareForReuse{
[super prepareForReuse];
self.downloading = NO;
}
答案 1 :(得分:0)
每次进入屏幕时,都会调用您的cellForRowAtIndexPath。您还在那里创建下载按钮,因此每次单元格离开屏幕然后返回到屏幕时,都会重新创建下载按钮。