嗨,我的应用程序出现问题,表视图中单元格中的UILabel内存泄漏。每次我进入表视图并返回时,内存都会上升2mb,如果我再次进入该视图,内存会上升2mb。为什么我知道这是因为UILabel:-我用探查器检查了一下:僵尸和-如果我从单元格中删除UILabels,我就没有内存泄漏
首先,我定义了我的单元格:
@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;
}
编辑------------应Alok的要求,我正在编写创建视图的代码:
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按钮时,我在视图/控制器中什么也不做,只有我有:
- (void)viewWillDisappear:(BOOL)animated
{
[self.navigationController setToolbarHidden:YES animated:NO];
[super viewWillDisappear:animated];
}
请从您的代码中找到uitableview内存上的示例test.zip我对firstLabel、firstLabelValue进行了更改。。secondLabel,secondLabelV值。。。。第五通过使它们具有属性。评论了您在这些标签中设置文本的代码。此外,还有一个屏幕截图显示在推送和弹出的内存释放中。查看蓝色上下的内存模式。。。在cellForRowAtIndexPath中,通过propertys.put@autorelease在for循环中设置标签中的文本(firstLabel,firstLabelValue..secondLabel,secondLabelVlue…第五个)。您可以在按钮等其他控件中遵循此模式,以提高内存用你的字典数据核实一下,然后告诉我。
感谢
感谢