目前,我得到了我的主要tableview与单元格,这是从XML加载和填充。之后,我可以点击一个单元格并加载一个detailview(xib文件,通过主表格中的代码加载)。在detailview上,我可以单击下载按钮,并将一些文件异步下载到文档文件夹。然后我弹出视图,再次看到我的桌面视图。更新UITableViewCell上的UIProgressView
我现在的想法是查看tableview单元格中下载的进度。我在每个单元格上设置了一个uiprogressview,默认情况下它是隐藏的。我现在想在开始下载时看到进度条。我怎样才能做到这一点?
我尝试了一些performSelectorOnMainThread和一个选择器,它改变了我选择的单元格的进度(我在主表格中分配)。似乎是一个坏主意 - 没有工作。
有些想法该怎么做?
编辑2:我的解决方案的一些代码,注意!这不是真正的代码,我在这里删除了很多行。 ;随行所有版本等等。我认为解决的办法是非常脏,但我没有发现我的一个更好的解决方案...
一些重要的事情:
- cellDownloading:数组,它知道 每一个细胞是在 下载时刻
- progressDictionary:每谁知细胞
- cellDictionary的进展:每谁知细胞,在tableview中的cellsForRowAtIndexpath delegte填补了
我也无法复制整个代码,因为有很多与整个过程无关的事情,但是我给了你重要的步骤。 我的单元格是通过解析一个xml创建的,所以每个单元格在我的情况下都有一个setId。
////////////////// // Downloader.mm //////////////////
方法下载asynchron
-(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];
我们需要一部字典都知道我们现有的tableview单元格的indexpath。 填补他们在的cellForRowAtIndexPath委托
[cellDictionary setObject:indexPath forKey:[NSString stringWithFormat:@"%d",cellSetId]];
现在我们创建一个处理程序做服用点,如果我们收到了通知 处理程序首先,如果下载开始 我有哪知道每一个细胞 的每一个进度的进度词典我可以找出由SETID每一个细胞 - 也许u必须改变一点点
- (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]];
}
您是否使用setProgressBar函数? – Legolas 2011-06-03 14:34:21
我不知道这个函数?:o我只是在我的单元格上设置了一个uiprogressview,并使用cell.progressView.progress = x设置了进度。 – NDY 2011-06-03 14:42:10
Na。没关系。这只是UIProgressView的一个出口。你为什么不使用performSelectorOnMainThread? – Legolas 2011-06-03 14:50:57