2014-02-19 1 views
0

이것은 내가 염두에 두어야 할 것입니다. 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]; 
} 

답변

0

당신은 메서드를 재정의 봐야한다 prepareForReuse 귀하의 tableviewcell 하위 클래스 및 셀 일관성있는 상태로 시작하는 것을 확인하십시오.

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; 
} 
+0

고마워요, 그게 내가 의심 스럽지만 그것을 교정하는 방법을 알지 못했습니다. 이걸 어떻게 막을 수 있을지 생각해? – devdev101

+0

당신은 단지 "옳은 일을해야합니다." 당신의 경우. UITableViewCell을 서브 클래 싱 한 다음 다운로드 버튼과 진행보기를 @property (강하고 비 구조)로 잡고 시작합니다. DownloadButtonCell * ... 내부. 그런 다음 상태를 변경하면 이미 인스턴스화 된 뷰를 제거하고 tableviewcell 하위 클래스에 추가합니다. tableviewcell sublcass에서 - (void) prepareForReuse를 사용하여 상태를 올바른 것으로 다시 설정하십시오. – Jon

+0

죄송합니다. 새 줄을 추가하는 대신 설명을 추가하십시오. – Jon

0

화면에 나타날 때마다 cellForRowAtIndexPath가 호출됩니다. 다운로드 단추를 만들어서 셀이 화면에서 벗어난 다음 화면으로 돌아올 때마다 다운로드 단추가 새로 만들어집니다.

+0

나는 그것을 안다. 내가 아는 것은 이것을 해결하는 방법입니다! – devdev101

+0

@rashidasgari, 일부 코드를 사용하여 내 대답을 업데이트했습니다. 초기 응답은 == add_comment를 입력하고 새로운 행을 삽입하려고했기 때문에 잘립니다. – Jon

+0

@ 존 오케이 감사합니다. – devdev101