UILabel 的奇怪问题

发布于 2024-11-28 06:34:29 字数 1173 浏览 3 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

明媚如初 2024-12-05 06:34:29

我也遇到过这种问题,但我通过设置 timeLabels 的值解决了这个问题,如下所示:-

timeLabels.text = @"value";

而不是

timeLabels = @"value";

I also faced this kind of problem, but i had solved this problem by set value of timeLabels as following:-

timeLabels.text = @"value";

instead of

timeLabels = @"value";
赴月观长安 2024-12-05 06:34:29

您显示的代码片段是正确的,无论如何,我的猜测是您很可能存在内存问题,导致您的 UILabel 实例在执行 layoutSubviews 之前的某个时刻被释放,然后该内存被 NSString 重用,因此您会在那里得到错误。

根据我的经验,发生这种情况的最常见情况是用错误的值错误地覆盖 timeLabels 可能会产生相同的结果。这可以在类内或从另一个类(可能尝试设置标签值)完成。

如果您想进行简单的测试,请将

 NSLog(@"timeLabels address %x", timeLabels);

两者添加到 initlayoutSubviews 中以比较这两个值并查看它们是否不同(或者可能没有,在此例中)如果你会遇到内存损坏问题)。

您应该检查您的代码,如果需要更多帮助,请发布更多代码。

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 before layoutSubviews is executed, then that memory is reused by an NSString, 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

 NSLog(@"timeLabels address %x", timeLabels);

both to init and to layoutSubviews 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文