UILabel 错误释放到 UITableViewCell 中

发布于 2024-11-24 23:57:02 字数 2563 浏览 1 评论 0原文

我有一个像这样的自定义 UITableViewCel:

@synthesize poiNameLabel;
@synthesize poiDistanceLabel;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
        // Initialization code
        CGRect nlabelframe = CGRectMake(NAMELABEL_X, NAMELABEL_Y, NAMELABEL_WIDTH, NAMELABEL_HEIGHT);
        UILabel *nlabel = [[UILabel alloc] initWithFrame:nlabelframe];
        nlabel.font = [UIFont fontWithName:@"Arial" size:NAMELABEL_FONT];
        nlabel.backgroundColor = [UIColor clearColor];
        nlabel.minimumFontSize = NAMELABEL_FONT_MIN;
        //nlabel.adjustsFontSizeToFitWidth = YES;
        [self addSubview:nlabel];
        self.poiNameLabel = nlabel;
        [nlabel release];

        CGRect dlabelframe = CGRectMake(DISTANCELABEL_X, DISTANCELABEL_Y, DISTANCELABEL_WIDTH, DISTANCELABEL_HEIGHT);
        UILabel *dlabel = [[UILabel alloc] initWithFrame:dlabelframe];
        dlabel.font = [UIFont fontWithName:@"Arial" size:DISTANCELABEL_FONT];
        dlabel.backgroundColor = [UIColor clearColor];
        dlabel.minimumFontSize = DISTANCELABEL_FONT_MIN;
        dlabel.adjustsFontSizeToFitWidth = YES;
        [self addSubview:dlabel];
        self.poiDistanceLabel = dlabel;
        [dlabel release];

        self.contentView.backgroundColor = [UIColor clearColor];
        self.contentView.opaque = NO;

    }
    return self;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [super setSelected:selected animated:animated];

}

- (void)dealloc {
    [super dealloc];
    [poiNameLabel release];
    [poiDistanceLabel release];
}

这就是我填充它的方式:

NSDictionary *dic = [[NSDictionary alloc] initWithDictionary:[poisSource objectAtIndex:row]];
ResultsViewCell *cell = [[[ResultsViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.poiNameLabel.text = [dic objectForKey:@"name"];
float distance = [[dic objectForKey:@"distance"] floatValue];
NSString *distanceWithUnity;
distance = distance*1000;
if (distance > 1000) {
    distanceWithUnity = [NSString stringWithFormat:@"%.2f km", distance/1000];
} else {
    distanceWithUnity = [NSString stringWithFormat:@"%.0f mt", distance];
}
cell.poiDistanceLabel.text = distanceWithUnity;
[dic release];
return cell;

一切都很好,但是当 UITableCell 被释放时,我得到 BAD_ACCESS 释放 poiDistanceLabel。
我发现我的代码没有问题,没有保留错误,所以我无法理解发生了什么。
我唯一的疑问是如何设置标签文本:这会是问题吗?为什么会发生这种情况?

I have a custom UITableViewCel like this:

@synthesize poiNameLabel;
@synthesize poiDistanceLabel;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
        // Initialization code
        CGRect nlabelframe = CGRectMake(NAMELABEL_X, NAMELABEL_Y, NAMELABEL_WIDTH, NAMELABEL_HEIGHT);
        UILabel *nlabel = [[UILabel alloc] initWithFrame:nlabelframe];
        nlabel.font = [UIFont fontWithName:@"Arial" size:NAMELABEL_FONT];
        nlabel.backgroundColor = [UIColor clearColor];
        nlabel.minimumFontSize = NAMELABEL_FONT_MIN;
        //nlabel.adjustsFontSizeToFitWidth = YES;
        [self addSubview:nlabel];
        self.poiNameLabel = nlabel;
        [nlabel release];

        CGRect dlabelframe = CGRectMake(DISTANCELABEL_X, DISTANCELABEL_Y, DISTANCELABEL_WIDTH, DISTANCELABEL_HEIGHT);
        UILabel *dlabel = [[UILabel alloc] initWithFrame:dlabelframe];
        dlabel.font = [UIFont fontWithName:@"Arial" size:DISTANCELABEL_FONT];
        dlabel.backgroundColor = [UIColor clearColor];
        dlabel.minimumFontSize = DISTANCELABEL_FONT_MIN;
        dlabel.adjustsFontSizeToFitWidth = YES;
        [self addSubview:dlabel];
        self.poiDistanceLabel = dlabel;
        [dlabel release];

        self.contentView.backgroundColor = [UIColor clearColor];
        self.contentView.opaque = NO;

    }
    return self;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [super setSelected:selected animated:animated];

}

- (void)dealloc {
    [super dealloc];
    [poiNameLabel release];
    [poiDistanceLabel release];
}

and this is how I fill it:

NSDictionary *dic = [[NSDictionary alloc] initWithDictionary:[poisSource objectAtIndex:row]];
ResultsViewCell *cell = [[[ResultsViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.poiNameLabel.text = [dic objectForKey:@"name"];
float distance = [[dic objectForKey:@"distance"] floatValue];
NSString *distanceWithUnity;
distance = distance*1000;
if (distance > 1000) {
    distanceWithUnity = [NSString stringWithFormat:@"%.2f km", distance/1000];
} else {
    distanceWithUnity = [NSString stringWithFormat:@"%.0f mt", distance];
}
cell.poiDistanceLabel.text = distanceWithUnity;
[dic release];
return cell;

Everything is ok, but when the UITableCell is deallocated, I get BAD_ACCESS releasing poiDistanceLabel.
I see no problem in my code, no retain errors, so I can't understand what's happening.
The only doubt I have is about how I'm setting the label text: can this be the issue? Why is this happening?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

¢好甜 2024-12-01 23:57:02

问题似乎出在您的 dealloc 方法中:

[super dealloc]

应该是您的 dealloc 方法中的最后一个调用,以在释放对象时保持对象“活动”。

Problem seems to be in your dealloc method:

[super dealloc]

should be the last call in your dealloc method to keep object "alive" while it is deallocating.

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