我正在异步下载图像并在UITableView中显示它们。下载图像时,UIProgressView应显示在相应的表格行中。下载完成后,进度视图应替换为实际图像。
我正在使用故事板,我将UITableViewCell子类化为一个名为ResponseTableViewCell的类,它有一个用于UIImageView responseView的IBOutlets和一个UIProgressView(progressView)。出于某种原因,我无法在下载完成后设法隐藏进度视图。如果我不隐藏它,它将显示在下载的图像的顶部。如果我试图隐藏它,它会隐藏在每一行中。我猜这与重复使用细胞有关。
我还尝试创建两个自定义单元格:一个使用UIImageView,另一个使用UIProgressView。但是在故事板中,我可以添加UITableViewCell的唯一方法是将它拖到UITableView上,这意味着我有两个UITableViewCells彼此叠加。
似乎工作的唯一方法是以编程方式创建图像视图和进度视图,而不是继承UITableViewCell,但我不想这样做。这是我的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ResponseTableViewCell *cell = (ResponseTableViewCell*)
[tableView dequeueReusableCellWithIdentifier:@"ResponseCell"];
if(indexPath.row == 0) {//display the main image
cell.responseView.image = _image;
[cell.progressView setHidden:YES];
return cell;
}
//display responses to the image
indexPath = [NSIndexPath indexPathForRow:indexPath.row-1 inSection:indexPath.section];
Photo *response = [ self getResponseAtIndex:indexPath.row];
NSMutableDictionary* downloadInfo = [self getConnectionInfoForId:[response photoId]];
if([response fullImage] == nil) {
float received = [[downloadInfo objectForKey:@"receivedBytes"] floatValue];
float total = [[downloadInfo objectForKey:@"totalFileSize"] floatValue];
NSNumber* percentage= [NSNumber numberWithFloat:received/total];
NSMutableDictionary* userInfo = [[NSMutableDictionary alloc] init];
[userInfo setObject:cell.progressView forKey:@"cell"];
[userInfo setObject:percentage forKey:@"percentage"]; /
[self performSelectorOnMainThread:@selector(updateProgressView:) withObject:userInfo waitUntilDone:NO
];
return cell;
}
else {
cell.responseView.image = response.fullImage;
return cell;
}
}
答案 0 :(得分:0)
我怀疑这可能与您发布的代码之外的内容有关。但另一个问题是
[cell.progressView setHidden:YES];
因为我看到你稍后在主线程上仔细更新了progressvView,但是你没有在主线程上执行setHidden
。这是一个UIProgressView,UI代码应该在主线程上,甚至可能只有setHidden
这么小的东西。