如何设置表格视图单元格附件视图以保留先前初始化的 UIImageView?

How to set the table view cell accessory view to retain a previously initialized UIImageView?(如何设置表格视图单元格附件视图以保留先前初始化的 UIImageView?)
本文介绍了如何设置表格视图单元格附件视图以保留先前初始化的 UIImageView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我的视图控制器中有一个属性,定义如下:

Let's say I have a property in my view controller, defined as follows:

@property (nonatomic, retain) UIImageView *checkmarkOffAccessoryView;

我在实现中@synthesize这个,在-deallocrelease它并在-viewDidLoad中初始化它如下:

I @synthesize this in the implementation, release it in -dealloc and initialize it in -viewDidLoad as follows:

self.checkmarkOffAccessoryView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checkmarkOff.png"]] autorelease];

到目前为止一切顺利.

当我在我的表格视图委托中使用它作为多个单元格的辅助视图时,会发生两件事:

When I use it in my table view delegate as an accessory view for multiple cells, two things happen:

  1. 只有一个单元格的辅助视图显示图像
  2. 应用程序 UI 冻结.

应用程序没有崩溃,据我所知,用户界面只是变得无响应.这在模拟器和设备上都有.

The app doesn't crash, as near as I can tell, the UI simply becomes unresponsive. This is both in the simulator and on the device.

这是我如何在单元格中使用初始化属性:

Here is how I use the initialized property with my cell:

- (UITableViewCell *) tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    // initialize or dequeue cell...

    if (condition)
        cell.accessoryView = self.checkmarkOffAccessoryView;
    else
        cell.accessoryView = nil;
}

使用上述代码,只有一个单元格显示附件视图并且 UI 冻结.

With the aforementioned code, only one cell shows the accessory view and the UI freezes.

如果我直接在委托方法中初始化 UIImageView 实例,我会得到所有满足条件的单元格显示附件视图,并且我不会遇到 UI 冻结:

If I initialize the UIImageView instance directly in the delegate method I get all condition-satisfying cells showing the accessory view and I do not experience the UI freeze:

- (UITableViewCell *) tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    // initialize or dequeue cell...

    if (condition)
        cell.accessoryView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checkmarkOff.png"]] autorelease];
    else
        cell.accessoryView = nil;
}

我的目标是初始化尽可能少的对象并重用一个 UIImageView.我很好奇为什么第一段代码有问题,我能做些什么来解决这个问题.

My goal is to initialize as few objects as possible and reuse one UIImageView. I'm curious why the first chunk of code is problematic and what I could do to fix this.

似乎单元格的 accessoryView 属性应该只是增加 self.checkmarkOffAccessoryViewretain 计数,但我似乎遗漏了一些细节.

It seems like the cell's accessoryView property should just increment the retain count of self.checkmarkOffAccessoryView but it appears I am missing some detail.

我忽略了什么?谢谢你的建议.

What have I overlooked? Thanks for your advice.

编辑

我认为:

self.checkmarkOffAccessoryView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checkmarkOff.png"]] autorelease];

等同于:

UIImageView *uncheckedView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checkmarkOff.png"]];
self.checkmarkOffAccessoryView = uncheckedView;
[uncheckedView release];

无论哪种方式,我都会遇到相同的冻结症状.

Either way, I experience the same freeze symptom.

推荐答案

不能多次添加同一个视图.UI 处理程序将变得疯狂.为了确保这一点,我尝试按照您上面所说的进行操作,但遇到了同样的问题.UI 冻结,图像仅出现在其中一个单元格中.

You cannot add the same view multiple times. The UI handler will go bonkers. To make sure of this, I tried doing what you said above and I got the same issue. The UI freezes up, the image only appears for one of the cells.

您可以做的最好的事情是将图像存储为分配的 UIImage,并拥有一个帮助函数,该函数会为每个单元格返回一个新的 UIImageView.

The best thing you can do is to store your image as a UIImage allocated, and to have a helper function which returns a new UIImageView per cell.

使用您当前的方法(没有存储的 UIImage)您可能会这样做:

Using your current method (without a stored UIImage) you might do:

-(UIImageView *) makeCheckmarkOffAccessoryView
{
    return [[[UIImageView alloc] initWithImage:
        [UIImage imageNamed:@"checkmarkOff.png"]] autorelease];
}

然后做

cell.accessoryView = [self makeCheckmarkOffAccessoryView];

您可能知道,另一方面,UIImages 可以使用任意次数.一个 UIImageView 不会占用太多空间,所以你可以轻松拥有一堆而不用担心.

As you may be aware, UIImages on the other hand may be used any number of times. a UIImageView doesn't take up a lot of space, so you can easily have a bunch of those without worrying.

要扩展一个地方的交易,假设您同时将一个 UIView 添加到两个地方.

To expand on the one place only deal, imagine that you add a UIView to two places at the same time.

[ob removeFromSuperview] 会为这个对象做什么?它会从两个地方删除视图吗?仅从其中之一?请求 [ob superview] 时会返回哪个值?显然,UI 不是用来处理您所要求的.

What will [ob removeFromSuperview] do for this object? Will it remove the view from both places? From one of them only? Which value will be returned when you request [ob superview]? Clearly the UI is not made to handle what you're asking for.

这篇关于如何设置表格视图单元格附件视图以保留先前初始化的 UIImageView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

The Google Mobile Ads SDK was initialized without AppMeasurement(Google 移动广告 SDK 在没有 AppMeasurement 的情况下进行了初始化)
No JNI_OnLoad found in ... skipping init(在 ... 中未找到 JNI_OnLoad 正在跳过初始化)
No JNI_OnLoad found skipping init gt; Application shutdown(没有找到跳过初始化的 JNI_OnLoad 应用程序关闭)
Fatal error: use of unimplemented initializer #39;init(coder:)#39; for class(致命错误:对类使用未实现的初始化程序“init(coder:))
Error in Swift class: Property not initialized at super.init call(Swift 类中的错误:在 super.init 调用时未初始化属性)
Conditional Binding: if let error – Initializer for conditional binding must have Optional type(条件绑定:if let error - 条件绑定的初始化程序必须具有可选类型)