2014-02-19 56 views
0

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

回答

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

Orite感謝,這是我懷疑自己,但不知道如何糾正它。你有什麼想法如何防止這種情況? – devdev101

+0

你只需要「做正確的事情」。爲你的情況。我將首先調用UITableViewCell,然後以@property(strong,nonatomic)DownloadButtonCell * ...的形式保存在您的下載按鈕和進度視圖中。然後,當你改變狀態刪除並添加tableviewcell子類中已經實例化的視圖。在你的tableviewcell sublcass中,然後使用 - (void)prepareForReuse將狀態重置爲正確的狀態。 – Jon

+0

對不起,輸入添加了評論,而不是添加新行。 – Jon

0

您的cellForRowAtIndexPath每次獲取屏幕時都會被調用。您還在那裏創建下載按鈕,因此每當單元格離開屏幕然後重新回到屏幕上時,下載按鈕將重新創建。

+0

我知道。我不知道如何解決這個問題! – devdev101

+0

@rashidasgari,我用一些代碼更新了我的答案,我的初始響應被切斷了,因爲輸入== add_comment,我試圖插入一個新行。 – Jon

+0

@Jon ok謝謝你看着它。 – devdev101