如何设置表视图单元附件视图以保留以前初始化的UIImageView? [英] How to set the table view cell accessory view to retain a previously initialized UIImageView?
问题描述
假设我的视图控制器中有一个属性,定义如下:
Let's say I have a property in my view controller, defined as follows:
@property (nonatomic, retain) UIImageView *checkmarkOffAccessoryView;
我 @synthesize
这在实施中, 发布
它在 -dealloc
中并在 -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
属性应该只增加 retain
计数 self.checkmarkOffAccessoryView
但似乎我遗漏了一些细节。
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]时会返回哪个值?很明显,用户界面并不能满足您的要求。
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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!