2012-02-16 43 views
1

地獄大家:)更新與上傳狀態的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表視圖決定刪除或重新使用這些上傳對象指向的單元格?它看起來不太安全。

+0

囉〜我想使用'ApplicationActivities'。但是,我不知道我添加框架。請告訴我這件事。 :-0謝謝! – hyekyung 2012-06-19 05:48:33

+0

ApplicationActivities是我自己的自定義類作爲項目的一部分:)它不是從任何外部框架。 – CodingBeagle 2012-06-25 08:40:29

回答

2

有兩種可能性,假設您有上傳後臺進程:

  1. 的實現代碼如下是代表和實現了一些上傳進度 功能
  2. 的實現代碼如下監聽上傳進度NSNotifications

第二個更容易實現,只需將偵聽器啓動/停止在viewdidappear/viewdiddissappear中即可。然後在您的上傳中,您可以跟蹤進度併發出附加用戶信息的通知,該通知會給出一個整數值以取得進展。該表具有處理接收到的此通知並重繪單元格的功能。 Here is how to add data to the userinfo part of an NSNotification

如果你想更有趣,你可以有一個上傳ID,並將其映射到一個單元格索引,並只重繪該特定單元格。 Here's a question and answers that explain how to do this.


噁心僞因爲我沒有訪問我的IOS開發ENV現在

上傳功能:

uploadedStuff{ 
    upload_id = ... // unique i, maps to row in table somehow 
    byteswritten = ... 
    bytestotal = .... 
    userinfo = new dict 
    userinfo["rowid] = upload_id 
    userinfo["progress"] = (int)byteswritten/bytestotal 
    sendNotification("uploadprogress",userinfo) 
} 

tableview.m:

viewdidappear{ 
    listenForNotification name:"uploadprogress" handledBy:HandleUploadProgress 
} 

viewdiddisappear{ 
    stoplisteningForNotification name:"uploadprogess" 
} 

HandleUploadProgess:NSNotification notification { 
userinfo = [notification userinfo] 
rowId = [userinfo getkey:"rowId"] 
progress = [userinfo getkey:"rowId"] 
// update row per the link above 
} 
+0

嗯,這似乎可能是一個可能的解決方案。你究竟如何在一個特定的單元中更新信息然後重新繪製它?我認爲這也是一個我並不確定如何處理它的最好方法。我還更新了我的帖子,提供了有關這些上傳對象如何調用其關聯UI控件的更多信息。 – CodingBeagle 2012-02-16 18:31:04

+0

我已經添加了一個鏈接來解釋如何重新繪製單獨的行。您需要在表格中執行的操作是將映射的字典上傳到表格行索引。我假設你的每個上傳都有一個唯一的標識符或名稱,所以我會使用它。然後在你的上傳功能中,你想發送一個包含兩條信息的userinfo字典的NSNotification,一個上傳唯一ID和進度百分比。此通知由解包通知用戶信息數據然後上載該特定行的功能處理。 – nflacco 2012-02-16 18:57:30

+0

啊哈,這似乎可以工作得很好:)非常感謝您的幫助!我會嘗試的。 – CodingBeagle 2012-02-16 19:20:26