我正在使用ASIHTTPRequest
下载文件。我的自定义UITableViewCell
有UIProgressView
,其中显示了对al下载的跟踪(每行一个)。我正在使用的单元格是自定义的,我覆盖了PrepareForReuse
方法,但是当我在tableview中进行滚动时,我发现了一个问题,单元格中的所有进度视图都被重置为方法PrepareforReuse
。
如何才能正确显示progressView?谢谢!
DownloadViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"DownloadsCell";
DownloadCell *cell = (DownloadCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[DownloadCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (indexPath.section == 0) {
cell.cellLabel.text = (NSString *)[names objectAtIndex:indexPath.row];
[cell.cellImageView setImage:[UIImage imageNamed:@"curso.png"] forState:UIControlStateNormal];
//cell.cellImageView.tag = indexPath.row;
ASIHTTPRequest *request;
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:[urls objectAtIndex:indexPath.row]]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[[[paths objectAtIndex:0] stringByAppendingPathComponent:@"Downloads"] stringByAppendingPathComponent:login] stringByAppendingPathComponent:[[download curso] ID]];
NSError *error;
if (![[NSFileManager defaultManager] fileExistsAtPath:path]){
if (![[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:NO attributes:nil error:&error]){
NSLog(@"Create directory 5 error: %@", error);
}
}else path = [path stringByAppendingPathComponent:[names objectAtIndex:indexPath.row]];
NSString *tmpPath = [[[paths objectAtIndex:0] stringByAppendingPathComponent:@"Downloads"] stringByAppendingPathComponent:@"TMP"];
NSError *error2;
if (![[NSFileManager defaultManager] fileExistsAtPath:tmpPath]){
if (![[NSFileManager defaultManager] createDirectoryAtPath:tmpPath withIntermediateDirectories:NO attributes:nil error:&error]){
NSLog(@"Create directory TMP error: %@", error2);
}
}
[request setDownloadDestinationPath:path];
[request setTemporaryFileDownloadPath:[[tmpPath stringByAppendingPathComponent:[names objectAtIndex:indexPath.row]]stringByAppendingString:@".download"]];
[request setAllowResumeForFileDownloads:YES];
[request setDownloadProgressDelegate:cell.cellProgressView];
//[request setDelegate:self];
[request setUserInfo:[NSDictionary dictionaryWithObjectsAndKeys:
[names objectAtIndex:indexPath.row], @"name",
[urls objectAtIndex:indexPath.row], @"url",
nil]];
[networkQueue addOperation:request];
NSLog(@"Downloading %d...",indexPath.row);
}
[networkQueue go];
return cell;
}
DownloadCell.m
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.contentView.frame = CGRectMake(0, 0, 320, 70);
cellImageView = [[UIButton alloc] initWithFrame:CGRectMake(5, 4, 58, 52)];
[self.contentView addSubview:cellImageView];
cellLabel = [[UILabel alloc] initWithFrame:CGRectMake(70, 10, 200, 30)];
cellLabel.backgroundColor = [UIColor clearColor];
cellLabel.textAlignment = NSTextAlignmentLeft;
cellLabel.textColor = [UIColor colorWithRed:1.0/255.0 green:19.0/255.0 blue:121.0/255.0 alpha:1.0];
cellLabel.font = [UIFont fontWithName:@"Gill Sans" size:13.0];
cellLabel.numberOfLines = 0;
[self.contentView addSubview:cellLabel];
cellProgressView = [[UIProgressView alloc] initWithFrame:CGRectMake(70, 40, 200, 15)];
[self.contentView addSubview:cellProgressView];
UIImageView * accesoryImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"flechaAzul.png"]];
self.accessoryView = accesoryImg;
}
return self;
}
- (void) prepareForReuse
{
self.contentView.frame = CGRectMake(0, 0, 320, 70);
self.contentView.backgroundColor = [UIColor whiteColor];
cellImageView.frame = CGRectMake(5, 4, 58, 52);
cellLabel.frame = CGRectMake(70, 10, 200, 30);
cellLabel.backgroundColor = [UIColor clearColor];
cellLabel.textAlignment = NSTextAlignmentLeft;
cellLabel.textColor = [UIColor colorWithRed:1.0/255.0 green:19.0/255.0 blue:121.0/255.0 alpha:1.0];
cellLabel.font = [UIFont fontWithName:@"Gill Sans" size:13.0];
cellLabel.numberOfLines = 0;
cellProgressView.frame = CGRectMake(70, 40, 200, 15);
UIImageView * accesoryImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"flechaAzul.png"]];
self.accessoryView = accesoryImg;
self.selectionStyle = UITableViewCellSelectionStyleBlue;
self.userInteractionEnabled = YES;
}
答案 0 :(得分:0)
当您致电[request setDownloadProgressDelegate:cell.cellProgressView];
时,request
应立即使用当前进度更新委托。如果它在发送任何更新之前等待进度发生变化,那么在发生这种情况之前,您将看到空的进度值。