這是我的想法。有一個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