UILabel 的奇怪问题
我在 UITableView
中使用 UILabel 作为自定义单元格。这是我正在使用的所有代码:
头文件:
UILabel *timeLabels;
@property (nonatomic, retain) UILabel *timeLabels;
代码文件:
- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier
timeLabels=[[UILabel alloc] init];
timeLabels.textAlignment=UITextAlignmentLeft;
timeLabels.font=[UIFont boldSystemFontOfSize:12];
timeLabels.backgroundColor=[UIColor clearColor];
timeLabels.textColor=[UIColor blueColor];
- (void) layoutSubviews
frame=CGRectMake(boundsX+5, 5, 60, 45);
timeLabels.frame=frame;
[timeLabels release]
我在 timeLabels.frame=frame
; 上收到以下错误
2011-08-08 12:44:07.290 EncameoApp[2014:707] -[NSCFString setFrame:]: unrecognized selector sent to instance 0x136890
2011-08-08 12:44:07.361 EncameoApp[2014:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString setFrame:]: unrecognized selector sent to instance 0x136890'
这很奇怪,因为 timeLabels 不是 NSString,而是 UILabel !
谁能告诉我我在这里错过了什么?谢谢。
I am using UILabel for custom cells in my UITableView
. Heres all the code that I am using :
header file:
UILabel *timeLabels;
@property (nonatomic, retain) UILabel *timeLabels;
code file:
- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier
timeLabels=[[UILabel alloc] init];
timeLabels.textAlignment=UITextAlignmentLeft;
timeLabels.font=[UIFont boldSystemFontOfSize:12];
timeLabels.backgroundColor=[UIColor clearColor];
timeLabels.textColor=[UIColor blueColor];
- (void) layoutSubviews
frame=CGRectMake(boundsX+5, 5, 60, 45);
timeLabels.frame=frame;
[timeLabels release]
I am getting the following error on timeLabels.frame=frame
;
2011-08-08 12:44:07.290 EncameoApp[2014:707] -[NSCFString setFrame:]: unrecognized selector sent to instance 0x136890
2011-08-08 12:44:07.361 EncameoApp[2014:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString setFrame:]: unrecognized selector sent to instance 0x136890'
Which is pretty strange given that timeLabels is not a NSString, but rather a UILabel !
Can anyone please let me know what I missed here ? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我也遇到过这种问题,但我通过设置 timeLabels 的值解决了这个问题,如下所示:-
而不是
I also faced this kind of problem, but i had solved this problem by set value of timeLabels as following:-
instead of
您显示的代码片段是正确的,无论如何,我的猜测是您很可能存在内存问题,导致您的
UILabel
实例在执行layoutSubviews
之前的某个时刻被释放,然后该内存被 NSString 重用,因此您会在那里得到错误。根据我的经验,发生这种情况的最常见情况是用错误的值错误地覆盖
timeLabels
可能会产生相同的结果。这可以在类内或从另一个类(可能尝试设置标签值)完成。如果您想进行简单的测试,请将
两者添加到
init
和layoutSubviews
中以比较这两个值并查看它们是否不同(或者可能没有,在此例中)如果你会遇到内存损坏问题)。您应该检查您的代码,如果需要更多帮助,请发布更多代码。
The code snippet you show is correct, anyway my guess is that you have very possibly a memory problem that makes your
UILabel
instance to be released at some point beforelayoutSubviews
is executed, then that memory is reused by anNSString
, so you get the error there.In my experience, the most common case for this to happen is anyway erroneously overwriting
timeLabels
with the wrong value could produce the same result. This could be done within the class or from another class (that maybe tries to set the label value).If you want to make a simple test, add
both to
init
and tolayoutSubviews
to compare the two values and see that they differ (or maybe they don't, in this case you would have a memory corruption problem).You should inspect your code, and post more of it if you need more help.