2011-08-22 43 views
2

我有一個帶有自定義單元格的表格視圖,每個單元格都有圖像和一些必須從網頁解析的文本。我有一個操作隊列,它從頁面獲取數據,並在加載每個頁面的數據並在2個數組中存儲數據後,在tableviewcontroller中調用方法(void)addLoadedImageAndExcerpt:(NSString *)imgExc。我需要每個單元格刷新一次與它相關的圖像和文本加載到這兩個數組(名爲「articleExcerpts」和「imageDataObjects」)。(iPad/iPhone)在屏幕上刷新表格單元格

的方法如下:

- (void)addLoadedImageAndExcerpt:(NSString *)imgExc { 
NSArray *imgAndExcerpt = [imgExc componentsSeparatedByString:@"{|}"]; 
[articleExcerpts addObject:[imgAndExcerpt objectAtIndex:1]]; 

NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: [imgAndExcerpt objectAtIndex:0]]]; 
[imageDataObjects addObject:imageData]; 
//count how many rows have been loaded so far. 
loadedCount ++; 

[self.table reloadData];//table is a UITableView 

[imageData release]; 
} 

的問題是,我不能讓細胞改變,而他們在屏幕上。一旦我滾動,他們就會在屏幕上顯示正確的數據,但我無法讓他們改變。我嘗試了方法herehere,但它們不起作用。我試着爲相關行調用tableView:cellForRowAtIndexPath:並修改變量,但那並沒有解決任何問題,因爲該方法每次調用時都會創建一個新單元,並且不會獲取現有單元(我將發佈代碼進一步下降)。
使用[self.table reloadData]因爲我有現在似乎並沒有做任何事情,無論是,這真的讓我困惑...

tableView:cellForRowAtIndexPath:方法(我敢打賭,這個問題就在這裏。我不認爲我正確地創建我的自定義細胞)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"CustomizedCell"; 

CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 

    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 

    for (id currentObject in topLevelObjects){ 
     if ([currentObject isKindOfClass:[UITableViewCell class]]){ 
      cell = (CustomCell *) currentObject; 
      break; 
     } 
    } 
} 

// Configure the cell... 

//title 
cell.titleString = [titles objectAtIndex:indexPath.row]; 
//date 
cell.dateString = [dates objectAtIndex:indexPath.row]; 

//Photo. check if imageDataObjects array is complete up to the current row yet 
if (loadedCount > indexPath.row) { 
    if ([imageDataObjects objectAtIndex:indexPath.row] != @"NA") { 
     cell.imageData = [imageDataObjects objectAtIndex:indexPath.row]; 
    } else { 
     cell.imageData = NULL; 
    } 
} 

//Excerpt. check if loadedCount array is complete up to the current row yet 
if (loadedCount > indexPath.row) { 
    cell.exerptString = [articleExcerpts objectAtIndex:indexPath.row]; 
} 

return cell; 
} 

我錯過了什麼?

回答

0

據我瞭解,圖像正在加載,然後添加到圖像數組(imageDataObjects),並且行從不更新。

首先,您確定addLoadedImageAndExcrept方法是按順序添加圖像嗎?請記住,NSArray對象無終結,因此,如果您要爲行進一步添加圖像,則在先前圖像爲零時不會顯示。如果圖像來到零會發生什麼?陣列會突然結束。使用數組上的「count」方法檢查是否發生這種情況,添加虛擬對象或切換到字典。這可能無法解決您目前的問題,但需要考慮。 (*)

除此之外,如果圖像被正確加載,爲您的代碼無法正常工作(在我從代碼瞭解)的唯一原因,就是表您添加IBOutlet中,沒有連接

* 編輯:我注意到,你在該行檢查@「NA」(雖然我沒有看到它被設置),所以你可能已經認爲

+0

現在你提到它,它看起來像IBOutlet表沒有連接,所以這一定是問題。但是,我認爲IBOutlets僅用於界面構建器,並且我沒有爲該表使用.xib文件。如果是這樣的話,我怎樣才能讓「table」只用代碼連接? – AnonyMouse

+0

那麼,「IBOutlets」的想法是,它們通過「IB」(Interface-Builder,它與XCode合併之前的圖形編輯器的名稱)連接。 我假定您使用了IBOutlet,但如果您告訴我您沒有使用xib,請粘貼您用於創建表的代碼,以便我們可以看一看。 – Can

1

我以前曾經有過類似的問題,並且能(得到它的工作通過包括線

[table beginUpdates]; 
[table endUpdates]; 

在其中接收數據的方法結束這麼稱呼他們,一旦你有數據填充單元格)。

希望這對你也有效!

+0

謝謝你,我有一個dispatch_sync ......問題,即發佈表時,我無法加載圖片進入細胞的ImageView了=> EXC_BAD_ACCESS。有了這兩條線,問題就消失了! – Charles

1

嗯,我認爲你只能在主線程中與UI組件交互。 NSOperationQueue的東西在另一個線程中運行。與其說

[self.table reloadData] 

的嘗試

[self.table performSelectorOnMainThread:@selector(reloadData:) withObject:nil waitUntilFinished:NO]