2014-02-19 29 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