- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个 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/
我一直在尝试实现这一点,但找不到任何可以在删除它们时像 iPhone 应用程序抖动一样抖动它的代码! CABasicAnimation *animation =
我有一个代码在 UIImageView.isAnimating 为 false 之后执行一些操作 但有趣的是它永远不会出错,它总是处于 YES 状态。 "isAnimating return is f
我的 UIViewController 中有一个 UIImageView。我在上面设置了一个图像。我做了如下旋转。 - (void) runSpinAnimationOnView:(UIImageVi
我有一个带有图像的 UIImageView。我在显示之前旋转了图像,方法是将 UIImageView 的变换属性设置为 CGAffineTransformMakeRotation(angle),其中角
如果有解决我的问题的答案,请给我一个链接。否则这是我的问题: 我目前正在运行 Swift 2.0 (Xcode 7) Sprite Kit Game,想知道 UIImageView 是否可以跟随另一个
我不知道我的想法是否正确,但我的问题是我在 UITableView 中有自定义单元格(看起来像汽车),每个单元格包含 2 个 UIImageViews(车轮图像),我需要旋转当 UITableView
我想知道是否有办法使用透明的 UIImageView(框 C)来剪辑移动到其上方的某些图像。 在我的示例中,我将均为 UIImageView 的框 A 和 B 移向透明的框 C。一旦盒子发生碰撞,我只
我有一个带有固定图像的 UIImageView。我想在顶部添加第二个 UIImageView,我打算旋转它。我需要在运行时创建顶部图像。我找不到在哪里处理透明度。它在 UIImage 中还是在 UII
我有一个要求,我有一个 UIImageView 在另一个之上。当用户在其上“绘制”时,我需要使顶部透明。实际上,不是绘制用户将要做的事情,而是“删除”顶 View ,让下面的 View 显示出来。 我
您好,我有一个 UIImageView,它部分在屏幕上,部分在屏幕外。 我想围绕屏幕外的一个点( View 的中心)旋转 View 。 我该怎么做? fullRotationAnimation
我有两张图片。一个图像是矩形第二个是三角形我想使用一根手指或触摸事件从矩形图像的中心点旋转三角形图像。 最佳答案 好吧,如果您希望图像围绕矩形中心点以任意大小的圆旋转,那么数学会变得相当复杂,但是一个
我想知道是否可以将更多的 UIImageView 分组到一个 ImageView 中。例如,如果我有 4 个 imageView,并且我想以完全相同的方式为它们制作动画,我可以将它们组合成一个,然后为
我正在代码中创建多个 UIImageView,并且想要移动和删除我选择的任何 UIImageView。现在,我只能移动使用 UIPanGestureRecognizer 创建的最后一个 UIImage
现在,我已经让它能够在我的应用程序上移动图像、缩放图像、捏合图像……应有尽有。但我的问题是,当一个 UIImageView 位于另一个 UIImageView 时,我如何才能让应用程序注册? 最佳答案
我有一个 UIImageView 应该从 size (0,0) --> (93,75) 开始动画 我有以下内容 [UIView animateWithDuration:0.5 delay:0 op
我这里的问题应该是一个相当简单的问题。基本上,我试图将一个对象 (UIImageView) 从 A 点(它在 Storyboard中设置的位置)移动到我以编程方式定义的 B 点。 我第一次通过这个导致
我有一个带有包含图像的单元格的 UITableView。我想从服务器下载一个大图像并将其插入到我的单元格中。 我的 UITableViewCell 有一个固定的高度并且填满了屏幕的整个宽度。最好的解决
当用户选择新的图像过滤器时,我尝试对图像进行交叉淡入淡出。 所以我在第一个ImageView之上创建了第二个ImageView,并对顶部 View 的alpha进行动画处理以显示或隐藏底部 View
我有一个 UIScrollView,其中包含 n 个占位符 UIImageView。下面我有一个 StackView,其中包含 10 个图像,用户可以从中选择并将图像拖动到上面的特定占位符 Image
尝试设置“选定框”图形的边界的多种组合,该图形仅显示用户在 UIImageView 中触摸的位置。它仍然继续坚持 super View 坐标系 class ViewController: UIView
我是一名优秀的程序员,十分优秀!