0

您好我有我的应用程序与UILabel在表视图单元格上的应用程序的问题。每当我进入表格视图并返回时,内存以2mb上升,如果再次进入该视图,则以2mb上升。为什么我知道那是因为的UILabel的: - 我与探查检查了它:僵尸和 - 如果我从细胞中删除UILabels,我没有内存泄漏UILabel与ARC在单元内存泄漏

对于开始我定义我的手机:

@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; 
} 

的exaple setter方法如下:

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

在我看来,我的cellForRowAtIndexPath样子:

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

编辑------------ 对于阿洛克的要求,我puting创建视图的代码:

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

我做nothink鉴于/ CONTROLER当我触摸BAC按钮,只有我有:

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

,首先实现的dealloc,把休息point.then在后退按钮按下的时候:“如果我进入到表视图,后每一次”叫你-dealloc ??? (我认为这个视图控制器有作为子视图uitableview?), – Alok 2014-09-27 13:20:21

+0

我添加 - (void)dealloc控制器扩展UITableViewController和dealloc方法没有被调用。 – lukisp 2014-09-27 13:34:46

+1

它必须被调用,即使你正在使用ARC,你也可以imeplement -dealloc {//////释放特殊对象,比如通知,而不是你的情况,不要调用[super dealloc];你可以告诉我当你打开这个子类/扩展视图控制器时的流程,你如何打开它,以及如何关闭它(按下后退按钮), – Alok 2014-09-27 13:35:20

回答

0

请在uitableview memory from your code发现样品test.zip 210我已经在firstLabel,firstLabelValue..secondLabel,secondLabelVlue ....第五中进行了修改。通过使他们property.commented您的代码设置文本在这些labels.also有一个屏幕截图showinh内存释放推和弹出内存模式蓝色上下看... 在cellForRowAtIndexPath设置文本在您的标签(firstLabel, firstLabelValue..secondLabel,secondLabelVlue .... fifth)通过propertis.put @autorelease在for循环中。你可以按照这个模式在其他控制像按钮更多的内存改进 验证它与您的字典数据,让我知道。

谢谢

+0

我做了你的修改我的项目,仍然我泄漏UILabel – lukisp 2014-09-27 17:58:01

+0

@lukisp让讨论http://chat.stackoverflow.com/rooms/62044/ios-discussion – Alok 2014-09-27 18:03:10

0

我解决了问题。重点是Alok的帖子。 我添加了方法dealloc,但它从未被调用过。 然后我试图删除viewWillDisappear方法中的所有引用(将它们设为nil),并且当我这样做时,然后调用dealloc方法释放所有内存。

感谢