I have a custom cell which has two UILabel objects.
//AppEventCell.h
#import <UIKit/UIKit.h>
@interface AppEventCell : UITableViewCell
{
UILabel * titleLabel;
UILabel * periodLabel;
}
@property (nonatomic, retain) UILabel * titleLabel;
@property (nonatomic, retain) UILabel * periodLabel;
@end
//AppEventCell.m
#import "AppEventCell.h"
@implementation AppEventCell
@synthesize titleLabel, periodLabel;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 13, 275, 15)];
[self.contentView addSubview:titleLabel];
periodLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 33, 275, 15)];
[self.contentView addSubview:periodLabel];
}
return self;
}
@end
- (AppEventCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"NoticeTableCell";
AppEventCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[AppEventCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
[cell.titleLabel setText:((NSString *)[[listArray objectAtIndex:indexPath.row] valueForKey:KEY_TITLE])];
[cell.periodLabel setText:((NSString *)[[listArray objectAtIndex:indexPath.row] valueForKey:KEY_PERIOD])];
return cell;
}
Here’s a question. Where Should I release titleLabel and periodLabel? I think I should release them by myself. However, there is no dealloc() in AppEventCell(I created the method but it never called). I put the release in CellForRowAtIndexPath but error occured when cell reused.
Shouldn’t I release the objects?
1) Here you should release labels after adding them as subviews:
2)
deallocmethod should be called for your cells. It’s wrong that it is not called. Check if you are releasing yourtableViewand in- (AppEventCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
is another memory leak:Mark new
cellasautoreleasedobject.3) If cell is reused (
[tableView dequeueReusableCellWithIdentifier:CellIdentifier];) then you should callreleaseorautorelease.