带有ARC的cellForRowAtIndexPath中的内存泄漏

时间:2013-09-01 01:11:51

标签: ios automatic-ref-counting

我在启用了ARC的新应用程序中的cellForRowAtIndexPath中出现内存泄漏。 cellForRowAtIndexPath只显示UILabel。 如果我添加[myUIlabel release];我收到ARC错误: “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; 

}

2 个答案:

答案 0 :(得分:2)

您每隔一个单元格将index_label子视图添加到每个单元格。您最终会多次添加标签并增加内存使用量;但是,这不是内存泄漏,而是逻辑上的问题。当细胞被破坏时,将回收内存。

解决方案很简单:在UILabelcell XIBPrototype Cell代码部分内创建cell == nil。这些选项中哪一个是合适的取决于您编写应用程序的方式;我个人使用故事板和原型单元。

答案 1 :(得分: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;
}