【问题标题】:Confused about adding a subview which I set its layer's contents property to a UITableViewCell?对添加我将其图层的内容属性设置为 UITableViewCell 的子视图感到困惑?
【发布时间】:2014-02-01 00:22:21
【问题描述】:

出于性能考虑,我想在静态内存空间中缓存 CGImageRef。
所以我有一个静态变量,它缓存了如下的图像内容,

UIImage *tempImage = [UIImage imageNamed:@"icon.jpg"];
static CGImageRef imageRef = nil;
if (!imageRef) {
    imageRef = tempImage.CGImage;
}

在 UITableView 的委托方法中,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    UIImageView *tempImageView = [[UIImageView alloc] .....
    tempImageView.layer.contents = (__bridge id)imageRef;
    [cell.contentView addSubview:tempImageView]; // added this image into UITableViewCell as a SubView.
}

这将在初始显示时起作用,
但是如果我选择了某个单元格,该单元格上的图像将在我选择后消失。(也许有一些内部绘制操作支持 UITableViewCell),我猜是选择事件使单元格进入高亮状态以进行显示。

没有下面的代码如何解决这个问题?我不想直接将一个UIImage分配给一个UIImageView。

tempImageView.image = tempImageView;

【问题讨论】:

  • 如果你不想要tempImageView.image = tempImageView;,请创建静态UIImageView
  • 谢谢,它会正常工作,但我想完全理解为什么在上述实现中选择后它会消失。
  • 需要查看cellForRowAtIndexPath的完整代码。您的行可能与未分配此图像的单元格一起重复使用。

标签: ios objective-c uitableview


【解决方案1】:

在编写某些测试代码后,我已经了解了在Cell.Cgimageref在分配给ImageView的CellayErer属性的Cell.Cgimageref中发生的问题时会发生问题。选择后,在ContentView中添加到ContentView后不会消失。

也就是说,当作为参数传递的selected或highlight为YES时,UITableViewCell如果实现了自己的行为,就会将其所有子view的状态变为selected状态。由于UIImageView有几个突出显示的状态,它会调整呈现自己在内部。 因此,如果您的视图只有一种呈现状态,您可以在一个简单的 UIView 上设置 CALayer.contents,而不是 UIImageView。

如果我继承一个 UITableViewCell 并覆盖

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
     //do nothing
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
     //do nothing
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-04
    • 1970-01-01
    • 2014-11-03
    • 2012-04-02
    • 1970-01-01
    • 1970-01-01
    • 2013-07-26
    • 2018-05-05
    相关资源
    最近更新 更多