我正在异步下载图像并将它们显示在 UITableView 中。下载图像时,UIProgressView 应显示在相应的表格行中。下载完成后,进度视图应替换为实际图像。
在我的表格视图中,我使用了一个名为 ProgressTableViewCell 的自定义单元格,它是 UITableViewCell 的子类。它有一个 UIProgressView IBOutlet。
我从 NSURLConnection 创建了一个 NSOperation 并将它们添加到一个 NSOperationQueue 中。作为代表
接收数据
方法被调用,通知被发布到我的表格视图控制器以更新相应的表格行
reloadRowsAtIndexPaths
表视图的方法。我的 cellForRowAtIndexPath 对重新加载的行执行以下操作:
ProgressTableViewCell *cell = (ProgressTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"ProgressCell"];
float received = [[downloadInfo objectForKey:@"receivedBytes"] floatValue];
float total = [[downloadInfo objectForKey:@"totalFileSize"] floatValue];
NSNumber* percentage= [NSNumber numberWithFloat:received/total];
NSMutableDictionary* userInfo = [[NSMutableDictionary alloc] init];
NSLog(@"percentage %f", percentage.floatValue);
[userInfo setObject:cell forKey:@"cell"];
[userInfo setObject:percentage forKey:@"percentage"];
[self performSelectorOnMainThread:@selector(updateProgressView:) withObject:userInfo waitUntilDone:NO];
NSLog(@"received: %@", [downloadInfo objectForKey:@"receivedBytes"]);
NSLog(@"Progress: %f", cell.progressView.progress);
return cell;
updateProgressView 方法看起来像
- (void)updateProgressView :(NSMutableDictionary *)userInfo
{
ProgressTableViewCell* cell = [userInfo valueForKey:@"cell"];
NSNumber* progress = [userInfo valueForKey:@"percentage"];
[cell.progressView setProgress:progress.floatValue ];
NSLog(@"Progress after update: %f", cell.progressView.progress);
}
我正在更新主线程上的进度视图,我什至尝试将 waitUntilDone 设置为 YES 但无济于事。我的进度视图停留在零点。有时,当我在调试时,我会看到进度指示器发生了一些变化,这让我认为这可能是时间问题。但是如何解决呢?
编辑:这是 NSURLConnection 委托的 didReceiveData 方法:
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[_responseData appendData:data];
NSNumber* bytes = [NSNumber numberWithUnsignedInt:[data length]];
NSLog(@"received bytes:%d", [bytes intValue] );
NSMutableDictionary* userInfo = [[NSMutableDictionary alloc] init];
[userInfo setObject:_responseId forKey:@"responseId"];
[userInfo setObject:bytes forKey:@"receivedBytes"];
[self fireNotification: [NSNotification
notificationWithName:@"DidReceiveData"
object:self userInfo:userInfo]];
}
- (void)fireNotification :(NSNotification *)aNotification
{
[[NSNotificationCenter defaultCenter] postNotification:aNotification];
}
这是我的视图控制器获取通知的方法:
-(void) dataReceived:(NSNotification *)notification {
NSNumber* responseId = [[notification userInfo] objectForKey:@"responseId"];
NSNumber* bytes = [[notification userInfo] objectForKey:@"receivedBytes"];
NSMutableDictionary* downloadInfo = [self getConnectionInfoForId:responseId];
NSLog(@"received bytes:%ld for response %@", [bytes longValue], responseId );
NSNumber* totalBytes = [NSNumber numberWithInt:([bytes longValue] + [[downloadInfo objectForKey:@"receivedBytes"] longValue]) ];
[downloadInfo setObject:totalBytes forKey:@"receivedBytes"];
float received = [[downloadInfo objectForKey:@"receivedBytes"] floatValue];
float total = [[downloadInfo objectForKey:@"totalFileSize"] floatValue];
[downloadInfo setObject:[NSNumber numberWithFloat:received/total] forKey:@"progress"];
[self reloadRowForResponse:responseId];
}
我还按照建议在我的 cellForRowAtIndexpath 方法中添加了一个 nil 检查:
ProgressTableViewCell *cell = (ProgressTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"ProgressCell"];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ProgressCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
float received = [[downloadInfo objectForKey:@"receivedBytes"] floatValue];
float total = [[downloadInfo objectForKey:@"totalFileSize"] floatValue];
NSNumber* percentage= [NSNumber numberWithFloat:received/total];
NSMutableDictionary* userInfo = [[NSMutableDictionary alloc] init];
NSLog(@"cell:%@", cell);
NSLog(@"percentage %f", percentage.floatValue);
[userInfo setObject:cell forKey:@"cell"];
[userInfo setObject:percentage forKey:@"percentage"];
[self performSelectorOnMainThread:@selector(updateProgressView:) withObject:userInfo waitUntilDone:NO];
return cell;