2013-09-01 75 views
0

在啓用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; 

}

+5

你怎麼知道你有泄漏? – rdelmar

回答

2

您正在爲每個單元分配並添加index_label,因此它每次都在增加內存。您可以在(cell == nil)塊中創建index_label,併爲index_label分配一些標籤以每次訪問標籤以更新index_label的屬性。

解決

- (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]; 

     UILabel* index_label = [[UILabel alloc] initWithFrame: CGRectZero]; 
     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]; 
      [cell.contentView addSubview:index_label ]; 
     index_label.tag=TAG_VALUE; 
    } 

    // 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=[cell.contentView viewWithTag:TAG_VALUE]; 
    index_label.text = [NSString stringWithFormat: @"" ]; 
    index_label.frame=CGRectMake(x,y, w,h); 
    return cell; 
} 
+0

問題:viewWithTag導致錯誤:「不兼容的指針類型初始化'UILabel * __強'與類型的'uivIEW *'」 –

+0

表達式的解決方案是這樣的:UILabel * index_label =(UILabel *)[cell.contentView viewWithTag:UILABEL_TAG]; –

2

要添加index_label子視圖每個單元格,你出列單元格每次。您最終會多次添加標籤並增加內存使用量;然而,這不是內存泄漏,而是邏輯上的一個問題。內存將在細胞被破壞時回收。

解決方案很簡單:在cell XIB,Prototype Cellcell == nil代碼部分中創建UILabel。這些選項中哪一個是合適的取決於你編寫應用程序的方式;我個人使用原型單元格的故事板。

+0

噢,好的。我將在「cell == nil」中執行UILabel分配,並將指針存儲到UILabel,然後在更新單元格時使用addSubview UILabel指針。 –

+0

爲什麼不在'cell == nil'部分添加'UILabel',然後在出列時使用適當的文本更新'UILabel'(或者'''''或者正確的文本)? –

+0

同意。我將在單元格內分配UILabel == nil,稍後用viewWithTag查找它,然後離開我們。 –