我有一个像这样的自定义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。
我发现我的代码没有问题,没有保留错误,所以我无法理解发生了什么
我唯一的疑问是我如何设置标签文本:这可能是问题吗?为什么会这样?
答案 0 :(得分:4)
问题似乎出现在dealloc
方法中:
[super dealloc]
应该是dealloc
方法中的最后一次调用,以便在解除分配时保持对象“活着”。