目前,我得到了带有单元格的主表格视图,这些单元格是从 XML 加载并填满的。之后,我可以单击一个单元格并加载一个详细视图(xib 文件,通过主表视图中的代码加载)。在详细视图上,我可以单击下载按钮,一些文件会异步下载到文档文件夹。然后我弹出视图,我再次看到我的表格视图。
我现在的想法是在 tableview 单元格中查看下载进度。我在每个单元格上设置了一个 uiprogressview,默认情况下它是隐藏的。我现在想在开始下载时看到进度条。我怎样才能做到这一点?
我用 performSelectorOnMainThread 和一个选择器尝试了一些东西,它改变了我选择的单元格的进度(我在主表视图中分配)。似乎是个坏主意 - 没用。
一些想法如何做到这一点?
EDIT2:我的解决方案的一些代码,注意!这不是真正的代码,我删除了很多要插入的行。没有所有版本等。我认为解决方案真的很脏,但我没有为我找到更好的解决方案......
一些重要的事情:
- cellDownloading:知道当前正在下载的每个单元格的数组
- progressDictionary:知道每个单元格的进度
- cellDictionary:知道每个单元格,填充在 tableview 的 cellsForRowAtIndexpath 委托中
我也不能复制整个代码,因为有很多与整个过程无关,但我给你重要的步骤。我的单元格是通过解析 xml 创建的,因此在我的情况下,每个单元格都有一个 setId。
/////////////// // Downloader.mm ////////////////
异步下载方法
-(void)startAsynchronousDownload:(NSString*)path{
// generate notification
NSMutableDictionary *userInfo = [[NSMutableDictionary alloc] init];
NSNumber *tmpSetId = [NSNumber numberWithInteger:setId];
[userInfo setObject:tmpSetId forKey:@"setId"];
NSNotification* notification = [NSNotification notificationWithName:@"downloadStackNotification" object:self userInfo:userInfo];
[[NSNotificationCenter defaultCenter] postNotification:notification];
// add some infos...
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
[dictionary setObject:tmpSetId forKey:@"setId"];
// perhaps more here
// asynchronous download handling
NSOperationQueue *queue = [NSOperationQueue alloc] init];
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(downloadImagesWithDictionary:) object:dictionary];
// release
[operation release];
[dictionary release];
[userInfo release];
}
在我的示例中,选择器 downloadImagesWithDictionary 会下载一些图像,对于每个下载的图像,我都会发送一个通知。在我的情况下,每叠图像都有一个 setId。
[...]
NSMutableDictionary *userInfo = [[NSMutableDictionary alloc] init];
NSNumber *sliceCounter = [NSNumber numberWithInt:i];
NSNumber *amount = [NSNumber numberWithInt:amountOfImages];
NSNumber *tmpSetId = [NSNumber numberWithInteger:setId];
[userInfo setObject:sliceCounter forKey:@"actualImage"];
[userInfo setObject:amount forKey:@"allImages"];
[userInfo setObject:tmpSetId forKey:@"setId"];
NSNotification* notification = [NSNotification notificationWithName:@"downloadSliceNotification" object:self userInfo:userInfo];
[[NSNotificationCenter defaultCenter] postNotification:notification ];
[...]
/////////////////////////TableViewController.mm /////////////// ///////////
现在我们需要获取通知来更新我们的 tableview
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(downloadSliceNotification:) name:@"downloadSliceNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(downloadStackNotification:) name:@"downloadStackNotification" object:nil];
我们需要一个单元格字典来了解我们现有表格视图单元格的所有索引路径。将它们填充到 cellForRowAtIndexPath 委托中
[cellDictionary setObject:indexPath forKey:[NSString stringWithFormat:@"%d",cellSetId]];
现在,如果我们收到通知,我们将创建处理程序来做一些事情首先,如果下载开始,处理程序我有一个进度字典,它知道每个单元格的每个进度我可以通过 setId 识别每个单元格 - 也许你必须稍微改变一下少量
- (void)downloadStackNotification:(NSNotification*)notification {
NSDictionary *userInfo = [notification userInfo];
NSNumber *setId = [userInfo objectForKey:@"setId"];
NSNumber *progress = [NSNumber numberWithInt:0];
[progressDictionary setObject:progress forKey:[setId stringValue]];
// add cell to downloadlist
[cellDownloading addObject:[setId stringValue]];
}
现在对于每个下载步骤(在我的情况下是一个图像)。计算过程并更新字典,获取单元格更新进度
- (void)downloadSliceNotification:(NSNotification*)notification {
NSDictionary *userInfo = [notification userInfo];
NSNumber *sliceCounter = [userInfo objectForKey:@"actualImage"];
NSNumber *amount = [userInfo objectForKey:@"allImages"];
NSNumber *setId = [userInfo objectForKey:@"setId"];
NSNumber *progress = [NSNumber numberWithFloat:(100/[amount floatValue])*[sliceCounter floatValue]];
[progressDictionary setObject:progress forKey:[setId stringValue]];
NSIndexPath* indexPath = [cellDictionary objectForKey:[setId stringValue]];
CustomServerCell* cell = (CustomServerCell*)[self.tableView cellForRowAtIndexPath:indexPath];
NSMutableDictionary *downloadData = [[NSMutableDictionary alloc] init];
[downloadData setObject:cell forKey:@"cell"];
[downloadData setObject:sliceCounter forKey:@"actualImage"];
[downloadData setObject:amount forKey:@"allImages"];
[self performSelectorOnMainThread:@selector(updateProgressWithDictionary:) withObject:downloadData waitUntilDone:NO];
}
更新单元格
-(void)updateProgressWithDictionary:(NSMutableDictionary*)downloadData {
CustomServerCell* cell = (CustomServerCell*)[downloadData objectForKey:@"cell"];
NSNumber *sliceCounter = [downloadData objectForKey:@"actualImage"];
NSNumber *amount = [downloadData objectForKey:@"allImages"];
cell.progressView.hidden = FALSE;
NSNumber *tmpProgress = [progressDictionary objectForKey:[NSString stringWithFormat:@"%d",cell.progressView.tag]];
cell.progressView.progress = [tmpProgress floatValue]/100;
cell.statusLabel.text = [NSString stringWithFormat:@"download slice %d / %d",[sliceCounter integerValue],[amount integerValue]];
}