带有ARC的UILabel单元中的内存泄漏 [英] memory leak in cell with UILabel with ARC

查看:54
本文介绍了带有ARC的UILabel单元中的内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用存在问题,表格视图的单元格中的UILabel发生内存泄漏.每次进入表视图并返回时,内存都会增加2mb,如果再次进入该视图,则内存会增加2mb.为什么我知道这是因为UILabel: -我用profiler:zombies和 -如果我从单元格中删除UILabel,就没有内存泄漏

Hi i have problem with my app with memory leak on UILabel in cells on table view. Every time if I enter to table view and back, memory rise with 2mb, and if I enter to that view again it rise with next 2mb. Why I know that is because of UILabel : - I checked it with profiler:zombies and - If I remove UILabels from cell, I hadn't memory leaks

首先,我定义了我的单元格:

For start I defined my cell:

@interface ExpandableTableViewCell : UITableViewCell{
    BOOL hasData;

    NSString *documentTypeId;
    NSString *documentId;
    UILabel *label;
    UILabel *dayAndMonthLabel;
    UILabel *yearLabel;
    UIButton *iconButton;

    UILabel *firstLineLabel;
    UILabel *firstLineValue;

    UILabel *secondLineLabel;
    UILabel *secondLineValue;

    UILabel *thirdLineLabel;
    UILabel *thirdLineValue;

    UILabel *fourthLineLabel;
    UILabel *fourthLineValue;

    UILabel *fifthLineLabel;
    UILabel *fifthLineValue;
}

- (void) setLabelValue: (NSString*) value;
- (void) setDayAndMonthValue: (NSString*) value;
- (void) setYearValue: (NSString*) value;
- (void) setFirstLineLabelValue: (NSString*) value;
- (void) setFirstLineValue: (NSString*) value;
- (void) setSecondLineLabelValue: (NSString*) value;
- (void) setSecondLineValue: (NSString*) value;
- (void) setThirdLineLabelValue: (NSString*) value;
- (void) setThirdLineValue: (NSString*) value;
- (void) setFourthLineLabelValue: (NSString*) value;
- (void) setFourthLineValue: (NSString*) value;
- (void) setFifthLineLabelValue: (NSString*) value;
- (void) setFifthLineValue: (NSString*) value;

- (void) openCell;
- (void) closeCell;

- (UIButton*) getIconButton;

@property (nonatomic) BOOL hasData;
@property (nonatomic) NSString *documentTypeId;
@property (nonatomic) NSString *documentId;

@end

init方法如下:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.clipsToBounds = true;
        [self setHasData: NO];
        [self setSelectionStyle:UITableViewRowAnimationBottom];
        [self setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];

        iconButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [iconButton setFrame: CGRectMake(6, 24, 30, 30)];
        [iconButton setBackgroundColor: [UIColor clearColor]];
        [iconButton setImage:[UIImage imageNamed:@"invoiceIcon.png"] forState:UIControlStateNormal];
        [iconButton setImage:[UIImage imageNamed:@"invoiceIcon.png"] forState:UIControlStateDisabled];
        [iconButton setEnabled: YES];
        [iconButton addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchDown];
        [iconButton  addTarget:self action:@selector(buttonRelease:) forControlEvents:UIControlEventTouchUpInside];
        [iconButton  addTarget:self action:@selector(buttonRelease:) forControlEvents:UIControlEventTouchUpOutside];

        label = [[UILabel alloc] initWithFrame:CGRectMake(42, 4, 250, 16)];
        [label setBackgroundColor:[UIColor clearColor]];
        [label setTextColor: [UIColor  colorWithRed:105.0/255.0 green:154.0/255.0 blue:64.0/255.0 alpha:1.0]];
        [label setFont:[UIFont fontWithName:@"Helvetica-Bold" size:16.0]];

        // creation date
        dayAndMonthLabel = [[UILabel alloc] initWithFrame:CGRectMake(266, 4, 50, 10)];
        [dayAndMonthLabel setBackgroundColor:[UIColor clearColor]];
        [dayAndMonthLabel setTextColor: [UIColor lightGrayColor]];
        [dayAndMonthLabel setFont:[UIFont fontWithName:@"Helvetica" size:10.0]];
        [dayAndMonthLabel setTextAlignment:NSTextAlignmentRight];


        yearLabel = [[UILabel alloc] initWithFrame:CGRectMake(266, 17, 50, 10)];
        [yearLabel setBackgroundColor:[UIColor clearColor]];
        [yearLabel setTextColor: [UIColor lightGrayColor]];
        [yearLabel setFont:[UIFont fontWithName:@"Helvetica" size:10.0]];
        [yearLabel setTextAlignment:NSTextAlignmentRight];
        //------

        // 1 ----
        firstLineLabel = [[UILabel alloc] initWithFrame:CGRectMake(42, 26, 90, 14)];
        [firstLineLabel setBackgroundColor:[UIColor clearColor]];
        [firstLineLabel setTextColor: [UIColor  colorWithRed:73.0/255.0 green:107.0/255.0 blue:196.0/255.0 alpha:1.0]];
        [firstLineLabel setFont:[UIFont fontWithName:@"Helvetica" size:14.0]];
        firstLineValue = [[UILabel alloc] initWithFrame:CGRectMake(42, 44, 250, 14)];
        [firstLineValue setBackgroundColor:[UIColor clearColor]];
        [firstLineValue setTextColor: [UIColor  darkGrayColor]];
        [firstLineValue setFont:[UIFont fontWithName:@"Helvetica" size: 14.0]];
        [firstLineValue setTextAlignment:NSTextAlignmentLeft];

        // 2 ----
        secondLineLabel = [[UILabel alloc] initWithFrame:CGRectMake(42, 62, 90, 14)];
        [secondLineLabel setBackgroundColor:[UIColor clearColor]];
        [secondLineLabel setTextColor: [UIColor  colorWithRed:73.0/255.0 green:107.0/255.0 blue:196.0/255.0 alpha:1.0]];
        [secondLineLabel setFont:[UIFont fontWithName:@"Helvetica" size:14.0]];
        secondLineValue = [[UILabel alloc] initWithFrame:CGRectMake(98, 62, 180, 14)];
        [secondLineValue setBackgroundColor:[UIColor clearColor]];
        [secondLineValue setTextColor: [UIColor  darkGrayColor]];
        [secondLineValue setFont:[UIFont fontWithName:@"Helvetica" size:14.0]];
        [secondLineValue setTextAlignment:NSTextAlignmentRight];

        // 3 ----
        thirdLineLabel = [[UILabel alloc] initWithFrame:CGRectMake(42, 80, 90, 14)];
        [thirdLineLabel setBackgroundColor:[UIColor clearColor]];
        [thirdLineLabel setTextColor: [UIColor  colorWithRed:73.0/255.0 green:107.0/255.0 blue:196.0/255.0 alpha:1.0]];
        [thirdLineLabel setFont:[UIFont fontWithName:@"Helvetica" size:14.0]];
        thirdLineValue = [[UILabel alloc] initWithFrame:CGRectMake(98, 80, 180, 14)];
        [thirdLineValue setBackgroundColor:[UIColor clearColor]];
        [thirdLineValue setTextColor: [UIColor  darkGrayColor]];
        [thirdLineValue setFont:[UIFont fontWithName:@"Helvetica" size:14.0]];
        [thirdLineValue setTextAlignment:NSTextAlignmentRight];

        // 4 ----
        fourthLineLabel = [[UILabel alloc] initWithFrame:CGRectMake(42, 96, 90, 13)];
        [fourthLineLabel setBackgroundColor:[UIColor clearColor]];
        [fourthLineLabel setTextColor: [UIColor  colorWithRed:73.0/255.0 green:107.0/255.0 blue:196.0/255.0 alpha:1.0]];
        [fourthLineLabel setFont:[UIFont fontWithName:@"Helvetica" size:13.0]];
        fourthLineValue = [[UILabel alloc] initWithFrame:CGRectMake(98, 96, 180, 13)];
        [fourthLineValue setBackgroundColor:[UIColor clearColor]];
        [fourthLineValue setTextColor: [UIColor  darkGrayColor]];
        [fourthLineValue setFont:[UIFont fontWithName:@"Helvetica" size:13.0]];
        [fourthLineValue setTextAlignment:NSTextAlignmentRight];

        // 5 ----
        fifthLineLabel = [[UILabel alloc] initWithFrame:CGRectMake(42, 109, 90, 13)];
        [fifthLineLabel setBackgroundColor:[UIColor clearColor]];
        [fifthLineLabel setTextColor: [UIColor  colorWithRed:73.0/255.0 green:107.0/255.0 blue:196.0/255.0 alpha:1.0]];
        [fifthLineLabel setFont:[UIFont fontWithName:@"Helvetica" size:13.0]];
        fifthLineValue = [[UILabel alloc] initWithFrame:CGRectMake(98, 109, 180, 13)];
        [fifthLineValue setBackgroundColor:[UIColor clearColor]];
        [fifthLineValue setTextColor: [UIColor  darkGrayColor]];
        [fifthLineValue setFont:[UIFont fontWithName:@"Helvetica" size:13.0]];
        [fifthLineValue setTextAlignment:NSTextAlignmentRight];

        [self.contentView addSubview: iconButton];
        [self.contentView addSubview: label];

        [self.contentView addSubview: dayAndMonthLabel];
        [self.contentView addSubview: yearLabel];

        [self.contentView addSubview: firstLineLabel];
        [self.contentView addSubview: firstLineValue];
        [self.contentView addSubview: secondLineLabel];
        [self.contentView addSubview: secondLineValue];
        [self.contentView addSubview: thirdLineLabel];
        [self.contentView addSubview: thirdLineValue];
        [self.contentView addSubview: fourthLineLabel];
        [self.contentView addSubview: fourthLineValue];
        [self.contentView addSubview: fifthLineLabel];
        [self.contentView addSubview: fifthLineValue];

    }
    return self;
}

示例设置器方法如下:

- (void) setFirstLineValue: (NSString*) value
{
    [firstLineValue setText:value];
}

在我看来,cellForRowAtIndexPath看起来像:

and in my view I had cellForRowAtIndexPath looks:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"DOCUMENT_LIST_CELL";

    ExpandableTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil){
        cell = [[ExpandableTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.documentTypeId = [NSString stringWithFormat:@"%@", fisDocumentTypeId ];
        if(indexPath.item >= start && indexPath.item <= end){
            [cell setHasData:TRUE];
        } else {
            UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
            spinner.frame = CGRectMake(148, 27, 24, 24);
            spinner.tag = 1234567;
            [cell setHasData:FALSE];
            [cell.contentView addSubview: spinner];
            [spinner startAnimating];
        }
    } else {
        id viewToRemove = [cell.contentView viewWithTag:1234567];
        [viewToRemove removeFromSuperview];
        viewToRemove = nil;

        if(indexPath.item >= start && indexPath.item <= end){
            [cell setHasData:TRUE];
        } else {
            UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
            spinner.frame = CGRectMake(148, 27, 24, 24);
            spinner.tag = 1234567;
            [cell setHasData:FALSE];
            [cell.contentView addSubview: spinner];
            [spinner startAnimating];
        }
    }

    if(indexPath.item >= start && indexPath.item <= end){
        NSMutableDictionary *dataDict = [documentListData objectAtIndex: (indexPath.item - start)];
        NSDate *localizedDate = [isoDateFormatter dateFromString:[dataDict valueForKey:@"creationDate"]];
        [cell setLabelValue: [dataDict valueForKey:@"identifier"] != [NSNull null] ? [dataDict valueForKey:@"identifier"] : [dataDict valueForKey:@"id"] ];
        cell.documentId = [NSString stringWithFormat:@"%@",[dataDict valueForKey:@"id"] ];
        [cell setDayAndMonthValue: [dayAndMonthFormatter stringFromDate:localizedDate] ];
        [cell setYearValue: [yearFormatter stringFromDate:localizedDate] ];

        for(int i = 0; i < 5; i++){
            if( [dataDict objectForKey: [NSString stringWithFormat:@"line%i",i ] ] != nil ){
                NSMutableDictionary *lineDict = [dataDict objectForKey: [NSString stringWithFormat:@"line%i",i ] ];

                switch (i) {
                    case 0:
                        [cell setFirstLineLabelValue: [lineDict objectForKey:@"field"]];
                        [cell setFirstLineValue: [[NSString stringWithFormat:@"%@", [lineDict objectForKey:@"value"]]stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] ];
                        break;
                    case 1:
                        [cell setSecondLineLabelValue: [lineDict objectForKey:@"field"]];
                        [cell setSecondLineValue: [[NSString stringWithFormat:@"%@", [lineDict objectForKey:@"value"]] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] ];
                        break;
                    case 2:
                        [cell setThirdLineLabelValue: [lineDict objectForKey:@"field"]];
                        [cell setThirdLineValue: [[NSString stringWithFormat:@"%@", [lineDict objectForKey:@"value"]] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] ];
                        break;
                    case 3:
                        [cell setFourthLineLabelValue: [lineDict objectForKey:@"field"]];
                        [cell setFourthLineValue: [[NSString stringWithFormat:@"%@", [lineDict objectForKey:@"value"]] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] ];
                        break;
                    case 4:
                        [cell setFifthLineLabelValue: [lineDict objectForKey:@"field"]];
                        [cell setFifthLineValue: [[NSString stringWithFormat:@"%@", [lineDict objectForKey:@"value"]] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] ];
                        break;
                    default:
                        break;
                }
            }
        }

    } else {
        [cell setLabelValue: @""];
        [cell setDayAndMonthValue: @""];
        [cell setYearValue: @""];
        [cell setFirstLineLabelValue: @""];
        [cell setFirstLineValue: @""];
        [cell setSecondLineLabelValue: @""];
        [cell setSecondLineValue: @""];
        [cell setThirdLineLabelValue: @""];
        [cell setThirdLineValue: @""];
        [cell setFourthLineLabelValue:@""];
        [cell setFourthLineValue:@""];
        [cell setFifthLineLabelValue:@""];
        [cell setFifthLineValue:@""];
    }


    return cell;
}

编辑------------ 应Alok的要求,我放入了创建视图的代码:

EDIT ------------ For request of Alok, I'm puting code of creating view:

DocumentListViewController *documentListView = [[DocumentListViewController alloc] initWithStyle:UITableViewStylePlain documentTypeId:documentTypeId documentTypeName:translation documentTypeIcon:nil documentTypeCode: code andSyncField:syncField andSyncFieldTranslation:syncFieldTrans documentTypeData:documentTypeData];
    documentListView.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:documentListView animated:YES];

当我触摸bac按钮时,我没有在视图/控制器中显示任何声音,只有我有:

And I do nothink in view/controler when I touch bac button, only I have:

- (void)viewWillDisappear:(BOOL)animated
{
    [self.navigationController setToolbarHidden:YES animated:NO];
    [super viewWillDisappear:animated];
}

推荐答案

请在

please find sample test.zip on uitableview memory from your code i have made changes in firstLabel, firstLabelValue..secondLabel, secondLabelVlue....fifth. by making them property.commented your code of setting text in these labels.also there is a screen shot showinh memory release on push and pop see memory pattern in blue up and down... in cellForRowAtIndexPath setting text in your labels (firstLabel, firstLabelValue..secondLabel, secondLabelVlue....fifth) by propertis.put @autorelease in for loop. you can follow this pattern in other controls like button for more memory improvement verify it with your dictionary data and let me know.

谢谢

这篇关于带有ARC的UILabel单元中的内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆