이것은 내가 염두에 두어야 할 것입니다. TableView
이 있습니다. 각 셀에는 단추가 있습니다. 내가 탭하면 버튼이 제거되고 대신 ProgressView
이 서브 뷰됩니다. 문제는 테이블을 스크롤하지 않았을 때 모든 것이 제대로 작동하는 것 같습니다. 그러나 스크롤을하면 모든 것이 엉망이되어 버튼과 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];
}
고마워요, 그게 내가 의심 스럽지만 그것을 교정하는 방법을 알지 못했습니다. 이걸 어떻게 막을 수 있을지 생각해? – devdev101
당신은 단지 "옳은 일을해야합니다." 당신의 경우. UITableViewCell을 서브 클래 싱 한 다음 다운로드 버튼과 진행보기를 @property (강하고 비 구조)로 잡고 시작합니다. DownloadButtonCell * ... 내부. 그런 다음 상태를 변경하면 이미 인스턴스화 된 뷰를 제거하고 tableviewcell 하위 클래스에 추가합니다. tableviewcell sublcass에서 - (void) prepareForReuse를 사용하여 상태를 올바른 것으로 다시 설정하십시오. – Jon
죄송합니다. 새 줄을 추가하는 대신 설명을 추가하십시오. – Jon