我ASIHTTPRequest
用来下载文件。我的自定义UITableViewCell
有UIProgressView
显示所有下载跟踪的位置(每行一个)。我正在使用的单元格是自定义的,我覆盖了该PrepareForReuse
方法,但是当我在表格视图中滚动时,我观察到一个问题,单元格中的所有进度视图都为该方法重置PrepareforReuse
。
我该怎么做才能使 progressViews 正确显示?谢谢!!
下载ViewController.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;
}
下载Cell.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;
}