目前,我得到了我的主要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