这是我的想法。有一个TableView
。每个单元格都有一个按钮。当我点击它时,该按钮会被删除,而代之以子查看器ProgressView
。问题是,当我没有滚动表时,一切似乎都很好。但是在滚动时,所有事情都会搞砸,而按钮和顺序也会混淆在一起。TableViewCell错位滚动
下面是代码:
- (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];
}
Orite感谢,这是我怀疑自己,但不知道如何纠正它。你有什么想法如何防止这种情况? – devdev101
你只需要“做正确的事情”。为你的情况。我将首先调用UITableViewCell,然后以@property(strong,nonatomic)DownloadButtonCell * ...的形式保存在您的下载按钮和进度视图中。然后,当你改变状态删除并添加tableviewcell子类中已经实例化的视图。在你的tableviewcell sublcass中,然后使用 - (void)prepareForReuse将状态重置为正确的状态。 – Jon
对不起,输入添加了评论,而不是添加新行。 – Jon