gpt4 book ai didi

ios - UITableViewCells 中的 UIImageViews 正在缓存的单元格中重新创建自己

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:22:43 25 4
gpt4 key购买 nike

我有一个 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;
}

最佳答案

代码的 else 分支不会将 interstitialImage 图像添加到单元格。您应该更改代码以在单元格的构造函数中添加 UIImageView,这样您就必须在 cellForRowAtIndexPath 中设置图像,而不是添加 subview 。

UIImageView 添加到您的 CustomCell,将其设为私有(private),并为 cellForRowAtIndexPath 提供一个属性以供使用:

@interface CustomCell : UITableViewCell {
UIImageView *_interstitialImage;
}
@property (nonatomic,readonly) UIImageView *interstitialImage;
@end

@implementation CustomCell
-(id)initWithStyle:(UITableViewCellStyle) style reuseIdentifier:(NSString*)reuseId {
if (self = [super initWithStyle:style reuseIdentifier:reuseId]) {
_interstitialImage = [[UIImageView alloc] init];
[_interstitialImage setImage: nil];
[_interstitialImage setFrame: CGRectMake(10, cell.lineLabel.frame.size.height + cell.actorLabel.frame.size.height + 10, 150, 150)];
[cell addSubview: _interstitialImage];
}
return self;
}
-(UIImageView*)interstitialImage {
return _interstitialImage;
}
@end

现在您的 cellForRowAtIndexPath 将使用 cell.interstitialImage 而不是一直分配新的 UIImageView。这将避免多次添加图像:

if ([[cellsToContainInterstitialImages allKeys] containsObject: [thisLine.lineID stringValue]]) {
NSNumber *cellTypeNumber = [cellsToContainInterstitialImages valueForKey: [thisLine.lineID stringValue]];
kInterstitialCellType cellType = [cellTypeNumber intValue];

if (cellType == kInterstitialCellTypeFirst) {
[cell.interstitialImage setImage:[UIImage imageNamed: @"man.png"]];
} else if (cellType == kInterstitialCellTypeSecond) {
[cell.interstitialImage setImage:[UIImage imageNamed: @"woman.png"]];
} else {
[cell.interstitialImage setImage: [UIImage imageNamed: @"child.png"]];
}
} else {
[cell.interstitialImage setImage: nil];
}

关于ios - UITableViewCells 中的 UIImageViews 正在缓存的单元格中重新创建自己,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15161134/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com