UILabel 错误释放到 UITableViewCell 中
我有一个像这样的自定义 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题似乎出在您的
dealloc
方法中:应该是您的
dealloc
方法中的最后一个调用,以在释放对象时保持对象“活动”。Problem seems to be in your
dealloc
method:should be the last call in your
dealloc
method to keep object "alive" while it is deallocating.