1

我正在异步下载图像并将它们显示在 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;
4

3 回答 3

3

我认为每次调用委托方法时都重新加载表格单元格,这是一种错误的方法。您可以直接抓取可见单元格并直接更新进度指示器,而不是通过数据源。

我假设您有某种方法可以转换responseId为要更新的行的索引路径——假设indexPathForResponseID:在您的控制器中调用了它。无需重新加载单元格,您只需抓住单元格(如果它是可见的)并更新其进度指示器:

- (void)dataReceived:(NSNotification *)notification {

    ...

    float received = [[downloadInfo objectForKey:@"receivedBytes"] floatValue];
    float total = [[downloadInfo objectForKey:@"totalFileSize"] floatValue];

    NSIndexPath *cellPath = [self indexPathForResponseID:responseId];
    ProgressTableViewCell *cell = (ProgressTableviewCell *)[self.tableView cellForRowAtIndexPath:cellPath];
    if (cell) {
        // make sure you're on the main thread to update UI
        dispatch_async(dispatch_get_main_queue(), ^{ 
            [cell.progressView setProgress: (received / total)];
        }
    }
}

但是,您应该知道,如果您的下载量比可见单元格多,则此解决方案还不够——您还应该将每次下载的进度存储在数据源中的某个位置,以便如果表视图确实需要重新加载单元格(由于滚动),它知道如何设置进度指示器。

于 2012-11-30T19:22:24.247 回答
0

差不多2个月后回到这个问题,我似乎找到了解决方案。不确定这是否是好的做法,但每次更新进度时创建一个新的 UIProgressView 似乎可以解决我的问题。这是方法:

-(void)updateProgressView :(NSMutableDictionary *)userInfo
{
    ProgressTableViewCell* cell = [userInfo objectForKey:@"cell"];
    cell.backgroundColor =[UIColor darkGrayColor];
    NSNumber* progress = [userInfo objectForKey:@"percentage"];
    NSLog(@"Progress before update: %f", cell.progressView.progress);

    UIProgressView *pView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
    pView.frame = CGRectMake(150, 200, 150, 9);
    pView.progress = progress.floatValue;

    [cell.contentView addSubview:pView];
}

非常感谢大家的帮助。

于 2013-01-19T22:25:54.837 回答
0

我发现在很多这样的情况下,您认为自己拥有的单元格并不是正在更新的单元格。当您重新加载时,您的代码会从可重复使用的单元格中弹出一个单元格,这基本上是一个旧单元格。如果您重新加载,该单元格将被另一个单元格替换。(如果可重复使用的单元格返回 nil,您也没有包括分配新单元格)如果您的单元格滚动关闭,它将重新加载,因此您必须确保将进度条放在那里的单元格上,而不是一个曾经在那里。您可能希望 NSLog 记录您开始传输的单元格的地址,然后在每次更新进度时再次记录。您需要使用该索引路径处的当前单元格更新进度条。它可能不是您最初开始该过程的那个。

于 2012-11-29T21:44:38.837 回答