我有一個帶有自定義單元格的表格視圖,每個單元格都有圖像和一些必須從網頁解析的文本。我有一個操作隊列,它從頁面獲取數據,並在加載每個頁面的數據並在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];
}
的問題是,我不能讓細胞改變,而他們在屏幕上。一旦我滾動,他們就會在屏幕上顯示正確的數據,但我無法讓他們改變。我嘗試了方法here和here,但它們不起作用。我試着爲相關行調用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;
}
我錯過了什麼?
現在你提到它,它看起來像IBOutlet表沒有連接,所以這一定是問題。但是,我認爲IBOutlets僅用於界面構建器,並且我沒有爲該表使用.xib文件。如果是這樣的話,我怎樣才能讓「table」只用代碼連接? – AnonyMouse
那麼,「IBOutlets」的想法是,它們通過「IB」(Interface-Builder,它與XCode合併之前的圖形編輯器的名稱)連接。 我假定您使用了IBOutlet,但如果您告訴我您沒有使用xib,請粘貼您用於創建表的代碼,以便我們可以看一看。 – Can