【发布时间】:2013-02-16 03:43:11
【问题描述】:
我有一个UITableViewCell,我需要将插页式图片随机放入其中一些。这是有效的,但由于单元格的缓存,图像会在表格的下方重复出现。假设我已经预先决定将图像放在单元格 1、12 和 25 中,这些单元格将有它们的图像,但在单元格 1 之后,单元格 4、8 等也可能包含第一张图像。
我猜我需要做的是专门告诉cellForRowAtIndexPath 清除缓存并删除任何不符合添加图像标准的图像。我该怎么做呢?我已经在 if 语句上尝试了 else 来做出决定并分配图像。在那个else 中,我尝试将UIImageView 的图像设置为nil,但这并没有任何区别。
有没有其他方法可以清除缓存以防止这些图像定期在页面上发送垃圾邮件?这是我的cellForRowAtIndexPath 方法,当前在最后一个else 中的行对图像没有影响,因此(我认为)它们需要替换为有效的方法!
- (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath
{
// Make and allocate the cell if necessary.
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier: @"Cell"];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle: UITableViewCellStyleValue1 reuseIdentifier: @"Cell"];
}
cell.lineLabel.text = [[self.fetchedResultsController objectAtIndexPath: indexPath] line];
cell.actorLabel.text = [[self.fetchedResultsController objectAtIndexPath: indexPath] actor];
[cell.lineLabel sizeToFitMultipleLines];
// Get the size each label needs to be when constrained to set width and very long height (8000).
CGSize labelSize = [cell.lineLabel.text sizeWithFont: cell.lineLabel.font constrainedToSize: CGSizeMake(265, 8000)];
if ([[cellsToContainInterstitialImages allKeys] containsObject: [thisLine.lineID stringValue]]) {
UIImageView *interstitialImage = [[UIImageView alloc] init];
NSNumber *cellTypeNumber = [cellsToContainInterstitialImages valueForKey: [thisLine.lineID stringValue]];
kInterstitialCellType cellType = [cellTypeNumber intValue];
if (cellType == kInterstitialCellTypeFirst) {
interstitialImage = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @"man.png"]];
}
else if (cellType == kInterstitialCellTypeSecond) {
interstitialImage = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @"woman.png"]];
}
else {
interstitialImage = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @"child.png"]];
}
[interstitialImage setFrame: CGRectMake(10, cell.lineLabel.frame.size.height + cell.actorLabel.frame.size.height + 10, 150, 150)];
[cell addSubview: interstitialImage];
} else {
UIImageView *interstitialImage = [[UIImageView alloc] init];
[interstitialImage setImage: nil];
}
return cell;
}
【问题讨论】:
标签: ios objective-c caching uitableview