Ios 滚动时TableViewCell放错位置

Ios 滚动时TableViewCell放错位置,ios,tableview,tableviewcell,Ios,Tableview,Tableviewcell,这就是我的想法。有一个表视图。每个单元格都有一个按钮。当我点击它时,按钮被移除,而一个ProgressView被子视图取代。问题是,当我没有滚动表格时,一切似乎都正常。但在滚动时,一切都变得一团糟,按钮和ProgressViews顺序也变得混乱 代码如下: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSStrin

这就是我的想法。有一个
表视图
。每个单元格都有一个按钮。当我点击它时,按钮被移除,而一个
ProgressView
被子视图取代。问题是,当我没有滚动表格时,一切似乎都正常。但在滚动时,一切都变得一团糟,按钮和
ProgressView
s顺序也变得混乱

代码如下:

 - (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];
}

您应该考虑在tableviewcell子类中重写方法prepareForReuse,并确保单元格以一致的状态开始

看起来你总是在CellForRowatineXpath方法中添加下载按钮,这意味着当单元格被重用时,下载按钮总是被添加的,这意味着每个单元格最终可能会添加多个下载按钮。。。当您删除按钮以添加进度条时,不清楚在这种情况下要删除哪个按钮(这在apple文档中定义模糊)

另外,如果已经有一个进度视图添加到即将重用的单元格中,则不会删除该进度视图,因此在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;
}

您的cellForRowAtIndexPath每次出现在屏幕上都会被调用。您还在那里创建下载按钮,因此每次单元格离开屏幕,然后回到屏幕上时,下载按钮都会重新创建

谢谢,这是我自己怀疑的,但不知道如何纠正。你知道如何防止这种情况吗?你只需要“做正确的事情”。对于你的情况。首先,我将对UITableViewCell进行子分类,然后按住下载按钮,并将进度视图设置为@property(强,非原子)DownloadButtonCell*。。。里面。然后,当您更改状态时,在tableviewcell子类中删除并添加已经实例化的视图。在tableviewcell子容器中,然后使用-(void)prepareforeuse将状态重置为正确的状态。抱歉,请输入added the comment,而不是添加新行。added code sample。也许我并没有完全弥补,但这是问题的症结所在。。。然后,所有复杂的视图创建、添加都可以简单地找到单元格,将.downloading属性设置为正确的值。不起作用!我确切地知道我的代码有什么问题,但不知道如何修复它。我无法正确使用单元格重用。@rashidasgari,我用一些代码更新了我的答案,我的初始响应被切断,因为enter==add_comment,我正试图插入新行。