0

これは私が念頭に置いていることです。がありますTableView。各セルにはボタンがあります。それをタップすると、ボタンが削除され、ProgressView代わりにサブビューが表示されます。問題は、テーブルをスクロールしていないときはすべて正常に動作しているように見えることです。しかし、スクロールすると、すべてがめちゃくちゃになり、ボタンとProgressViews の順序がすべて混乱します。

コードは次のとおりです。

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

2 に答える 2

0

tableviewcell サブクラスでメソッド prepareForReuse をオーバーライドし、セルが一貫した状態で開始することを確認する必要があります。

cellForRowAtIndexpathメソッドにダウンロードボタンを常に追加しているようです。つまり、セルが再利用されると、ダウンロードボタンが常に追加されます。つまり、各セルに複数のダウンロードボタンが追加される可能性があります...ボタンを削除するとプログレスバーを追加するために、その場合にどのボタンを削除するかは明確ではありません (これは 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;
}
于 2014-02-19T16:38:07.433 に答える
0

cellForRowAtIndexPath は、画面に表示されるたびに呼び出されます。そこにダウンロード ボタンも作成しているため、セルが画面から消えて画面に戻るたびに、ダウンロード ボタンが新たに作成されます。

于 2014-02-19T17:11:54.750 に答える