我有一個自定義單元格,它有兩個UILabel對象。我應該在哪裏發佈自定義單元格對象?
//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;
}
這裏有一個問題。我應該在哪裏發佈titleLabel和periodLabel?我想我應該自己釋放他們。但是,AppEventCell中沒有dealloc()(我創建了該方法,但從未調用過)。我將該版本放入CellForRowAtIndexPath中,但在單元重用時發生錯誤。
不應該釋放對象嗎?
謝謝!!!!非常明確的答案。再次感謝。 – Ryan 2012-03-05 06:10:16