在啓用ARC的新應用程序中,cellForRowAtIndexPath中出現內存泄漏。 cellForRowAtIndexPath只顯示一個UILabel。 Buf it I add [myUIlabel release];我得到ARC錯誤: 「ARC禁止'發佈'的顯式消息發送」cellForRowAtIndexPath中帶有ARC的內存泄漏
如果我刪除UILabel,泄漏將消失。
我不想禁用ARC,因爲它使內存管理。更輕鬆。
解決方案是什麼?
下面的代碼...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
int row = indexPath.row;
float font_size;
UITextView* PWR_RX_cover_box;
int x,y,w,h;
// Determine which channel:
int channel = tableView.tag; // tag=channel, set at init time
// Prepare to update cell:
// DOCUMENTATION: Table View Programming Guide for iOS > Adding subviews to a cell’s content view
// Give each cell a cell identifier unique to each channel tableView and unique to each row, so that each gets a unique data structure:
NSString *CellIdentifier = [NSString stringWithFormat:@"%d_%d",channel,indexPath.row];
//static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// if nil: cell(chan, row) has not been created before. <>nil: cell = data structure previously initialized
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: CellIdentifier];
}
// Erase anything previously displayed in the cell, by drawing cell-size big, white label:
font_size = 10.0;
// Top, left corner of cell:
y = 0;
x = 0;
// Entire area of cell:
h = CHANNEL_ROW_HEIGHT; // height of cell
w = channel_tableView_width; // width of cell
UILabel* index_label = [[UILabel alloc] initWithFrame: CGRectMake(x,y, w,h)];
index_label.backgroundColor = [UIColor whiteColor];
index_label.textAlignment = NSTextAlignmentLeft; // NSTextAlignmentCenter, NSTextAlignmentLeft NSTextAlignmentRight
index_label.textColor=[UIColor darkGrayColor];
index_label.numberOfLines=1;
index_label.font = [UIFont systemFontOfSize: font_size];
index_label.text = [NSString stringWithFormat: @"" ];
//index_label.text = [NSString stringWithFormat: @" *LAST %d *", ++last_ind]; // normally ""
[cell.contentView addSubview:index_label ];
[index_label release]; <<<<<<<<<<<<<<<<<<< CAUSES ARC COMPILE ERROR
return cell;
}
你怎麼知道你有泄漏? – rdelmar