我有一个带有自定义单元格的表视图,每个单元格都有图像和一些必须从网页中解析的文本。我有和操作队列从页面获取数据并在每个页面的数据加载后调用tableviewcontroller中的方法(void)addLoadedImageAndExcerpt:(NSString *)imgExc
并将数据存储在2个数组中。一旦将与其关联的图像和文本加载到这两个数组(名为“articleExcerpts”和“imageDataObjects”)中,我需要刷新每个单元格。
方法如下:
- (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:
并修改变量,但这并没有解决任何问题,因为该方法似乎每次调用时都会创建一个新单元格,并且不会获取现有单元格(I') ll将该方法的代码进一步向下发布。)
现在使用[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 :(得分:1)
之前我遇到过类似的问题,并且能够通过包含行
来使其工作[table beginUpdates];
[table endUpdates];
在接收数据的方法结束时(一旦有数据填充单元格,请调用它们。)
希望这也适合你!
答案 1 :(得分:1)
嗯,我认为你只应该与主线程中的UI组件进行交互。 NSOperationQueue的东西在另一个线程中运行。而不是打电话
[self.table reloadData]
试
[self.table performSelectorOnMainThread:@selector(reloadData:) withObject:nil waitUntilFinished:NO]
答案 2 :(得分:0)
据我了解,正在加载图像,然后将其添加到图像数组(imageDataObjects),并且行永远不会更新。
首先,您确定addLoadedImageAndExcrept方法是按顺序添加图像吗?请记住 NSArray对象是nil-terminated ,因此,如果您要进一步添加一行图像,则如果之前的图像为零,则不会显示。如果图像为零,会发生什么?阵列将突然结束。使用数组上的“count”方法检查是否发生这种情况,添加虚拟对象或swtich到字典。这可能无法解决您当前的问题,但需要考虑。 (*)
除此之外,如果正确加载图片,您的代码无法工作的唯一原因(我从代码中理解)是您添加的表IBOutlet未连接
* 编辑:我注意到你在行上检查@“NA”(虽然我看不到它在哪里设置),所以你可能已经考虑了