问题描述
假设我的视图控制器中有一个属性,定义如下:
Let's say I have a property in my view controller, defined as follows:
@property (nonatomic, retain) UIImageView *checkmarkOffAccessoryView;
我在实现中@synthesize
这个,在-dealloc
中release
它并在-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:
- 只有一个单元格的辅助视图显示图像
- 应用程序 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.checkmarkOffAccessoryView
的 retain
计数,但我似乎遗漏了一些细节.
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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!