地獄大家:)更新與上傳狀態的UI表視圖細胞 - iOS設備
我與UITablewView控制器在iOS的經驗是不幸相當有限。我在我的應用程序中需要的是一個UI表格視圖,其中包含一個用於上傳到網絡服務器(視頻,音頻等)的每個活動上傳的自定義單元。
每個上傳的背景asynchrounously運行,都應該能夠更新的東西,如在各自的細胞UILabels說一些關於百分比更新進度等
現在我已經找到了解決辦法哪些工作。問題是我不知道它是否確實安全。根據我自己的結論,我並不真的認爲這是事實。我所做的只是從正在創建的單元格中檢索UIViews的引用,然後將這些引用存儲在上傳對象中,以便它們可以自己更改標籤文本等等。
自己的解決方案
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CustomCellIdentifier = @"CustomCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"UploadCellView" owner:self options:nil];
if ([nib count] > 0)
{
cell = customCell;
}
else
{
NSLog(@"Failed to load CustomCell nib file!");
}
}
NSUInteger row = [indexPath row];
UploadActivity *tempActivity = [[[ApplicationActivities getSharedActivities] getActiveUploads] objectAtIndex:row];
UILabel *cellTitleLabel = (UILabel*)[cell viewWithTag:titleTag];
cellTitleLabel.text = tempActivity.title;
UIProgressView *progressbar = (UIProgressView*)[cell viewWithTag:progressBarTag];
[progressbar setProgress:(tempActivity.percentageDone/100) animated:YES];
UILabel *cellStatusLabel = (UILabel*)[cell viewWithTag:percentageTag];
[cellStatusLabel setText:[NSString stringWithFormat:@"Uploader - %.f%% (%.01fMB ud af %.01fMB)", tempActivity.percentageDone, tempActivity.totalMBUploaded, tempActivity.totalMBToUpload]];
tempActivity.referencingProgressBar = progressbar;
tempActivity.referencingStatusTextLabel = cellStatusLabel;
return cell;
}
正如你所看到的,這是我覺得我做的事情是不是很夠好: tempActivity.referencingProgressBar =進度; tempActivity.referencingStatusTextLabel = cellStatusLabel;
上傳活動得到存儲在該單元格的控件的引用,然後可以更新他們自己。問題是我不知道這是否安全。如果他們正在引用的單元格被重新使用或從內存中刪除,等等呢?
是否有另一種方式,你可以簡單地更新底層模型(我上傳的活動費),然後迫使UI表視圖重繪改變細胞?你最終能否繼承UITableViewCell的子類並讓它們不斷地檢查上傳,然後讓它們自己上傳?
編輯
這是上載活動的對象是如何調用其引用的UI控件:
- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten
totalBytesWritten:(NSInteger)totalBytesWritten
totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite
{
if (referencingProgressBar != nil)
{
[referencingProgressBar setProgress:(percentageDone/100) animated:YES];
}
if (referencingStatusTextLabel != nil)
{
[referencingStatusTextLabel setText:[NSString stringWithFormat:@"Uploader - %.f%% (%.01fMB ud af %.01fMB)", percentageDone, totalMBUploaded, totalMBToUpload]];
}
}
我唯一擔心的是,由於這些對象asynchrounously運行,如果在某個給定的點的UI表視圖決定刪除或重新使用這些上傳對象指向的單元格?它看起來不太安全。
囉〜我想使用'ApplicationActivities'。但是,我不知道我添加框架。請告訴我這件事。 :-0謝謝! – hyekyung 2012-06-19 05:48:33
ApplicationActivities是我自己的自定義類作爲項目的一部分:)它不是從任何外部框架。 – CodingBeagle 2012-06-25 08:40:29