Tableview增加内存?

时间:2016-04-21 12:13:07

标签: ios objective-c iphone uitableview

我创建了一个自定义的tableview。除此之外,我还有一些简单的tableview。当我从上一个表视图转到下一个自定义表视图时,内存增加。下面是自定义tableview的代码。请检查。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *simpleTableIdentifier = @"ComponentCell";

    ClientAssessmentComponentCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {

        cell = [[ClientAssessmentComponentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
    cell.accessoryType = UITableViewCellAccessoryNone;
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    SubComponent *componentInfo = [self.componentsData objectAtIndex:indexPath.row];
    cell.component_name.text = componentInfo.componentName;
    NSLog(@"status %d --- %@ ---- %@ ---- %@",(int)componentInfo.componentStatus,componentInfo.componentAllImages,componentInfo.componentNotes,componentInfo.componentScore);
    cell.tag = indexPath.row;
    cell.dirty_btn.tag = indexPath.row;
    cell.camera_img.tag = indexPath.row;
    cell.count_img.tag = indexPath.row;
    cell.count_lbl.tag = indexPath.row;
    cell.notes_img.tag = indexPath.row;
    cell.count_lbl.hidden = true;
    cell.count_img.hidden = true;
    cell.count_lbl.textColor = [UIColor whiteColor];
    cell.count_lbl.textAlignment = NSTextAlignmentCenter;
    cell.count_lbl.adjustsFontSizeToFitWidth = YES;
    cell.a_btn.tag = indexPath.row;
    cell.b_btn.tag = indexPath.row;
    cell.c_btn.tag = indexPath.row;
    cell.d_btn.tag = indexPath.row;
    cell.e_btn.tag = indexPath.row;
    cell.f_btn.tag = indexPath.row;
    [cell.dirty_btn setBackgroundColor:[UIColor colorWithRed:(138/256.0) green:(138/256.0) blue:(138/256.0) alpha:1]];
    [cell.a_btn setImage:[UIImage imageNamed:@"radio.png"]forState:UIControlStateNormal];
    [cell.b_btn setImage:[UIImage imageNamed:@"radio.png"]forState:UIControlStateNormal];
    [cell.c_btn setImage:[UIImage imageNamed:@"radio.png"]forState:UIControlStateNormal];
    [cell.d_btn setImage:[UIImage imageNamed:@"radio.png"]forState:UIControlStateNormal];
    [cell.e_btn setImage:[UIImage imageNamed:@"radio.png"]forState:UIControlStateNormal];
    [cell.f_btn setImage:[UIImage imageNamed:@"radio.png"]forState:UIControlStateNormal];
    UITapGestureRecognizer *notes_tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(notesView:)];
    notes_tap.numberOfTapsRequired = 1;
    [cell.notes_img addGestureRecognizer:notes_tap];

    UITapGestureRecognizer *image_tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(takePhoto:)];
    notes_tap.numberOfTapsRequired = 1;
    [cell.camera_img addGestureRecognizer:image_tap];

    [cell.dirty_btn addTarget:self action:@selector(do_later_button_click1:) forControlEvents:UIControlEventTouchUpInside];
    [cell.a_btn addTarget:self action:@selector(a_button_click1:) forControlEvents:UIControlEventTouchUpInside];
    [cell.b_btn addTarget:self action:@selector(b_button_click1:) forControlEvents:UIControlEventTouchUpInside];
    [cell.c_btn addTarget:self action:@selector(c_button_click1:) forControlEvents:UIControlEventTouchUpInside];
    [cell.d_btn addTarget:self action:@selector(d_button_click1:) forControlEvents:UIControlEventTouchUpInside];
    [cell.e_btn addTarget:self action:@selector(e_button_click1:) forControlEvents:UIControlEventTouchUpInside];
    [cell.f_btn addTarget:self action:@selector(f_button_click1:) forControlEvents:UIControlEventTouchUpInside];
    if([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPad)
    {
        cell.component_name.font = [cell.component_name.font fontWithSize:22.0];
        cell.dirty_btn.titleLabel.font = [cell.dirty_btn.titleLabel.font fontWithSize:18.0];
    }
    else
    {
        cell.component_name.font = [cell.component_name.font fontWithSize:18.0];
        cell.dirty_btn.titleLabel.font = [cell.dirty_btn.titleLabel.font fontWithSize:14.0];
    }
    if(componentInfo.componentNotes != nil && componentInfo.componentNotes != NULL && ![componentInfo.componentNotes isEqual:@"(null)"] && ![componentInfo.componentNotes isEqual:@"null"])
    {
        cell.notes_img.image = [UIImage imageNamed:@"notes-with-glow.png"];
    }
    if(componentInfo.componentAllImages != nil && componentInfo.componentAllImages != NULL && ![componentInfo.componentAllImages isEqual:@"(null)"] && ![componentInfo.componentAllImages isEqual:@"null"] && ![componentInfo.componentAllImages isEqual:@""] )
    {
        cell.camera_img.image = [UIImage imageNamed:@"camera-with-glow.png"];
        NSArray *image_arr = [componentInfo.componentAllImages componentsSeparatedByString:@", "];
        cell.count_img.hidden = NO;
        cell.count_lbl.hidden = NO;
        cell.count_lbl.text = [NSString stringWithFormat:@"%d",(int)[image_arr count]];
        for(int x = 0; x < [image_arr count]; x++){
            NSString *imageName =[image_arr objectAtIndex:x];
            if([imageName containsString:@"http:"])
            {
                NSArray *imgNameSplit = [imageName componentsSeparatedByString:@"/"];
                NSLog(@"count is %lu",(long)[imgNameSplit count]);
                imageName = [imgNameSplit objectAtIndex:[imgNameSplit count]-1];
            }
            if(x == 0){
                self.componentImage.componentAllImages = imageName;
                self.componentImage.imageName =[[NSMutableArray alloc]init];
                [self.componentImage.imageName addObject:imageName];
                self.componentImage.imageClickedTime = [[NSMutableArray alloc]init];
                NSString *image_clicked_time = [self getClickTime];
                [self.componentImage.imageClickedTime addObject:image_clicked_time];
                self.componentImage.componentAllImagesTime = image_clicked_time;
            }
            else{
                self.componentImage.componentAllImages = [self.componentImage.componentAllImages stringByAppendingString:@", " ];
                self.componentImage.componentAllImages = [self.componentImage.componentAllImages stringByAppendingString:imageName];
                [self.componentImage.imageName addObject:imageName];
                NSString *image_clicked_time = [self getClickTime];
                [self.componentImage.imageClickedTime addObject:image_clicked_time];
                self.componentImage.componentAllImagesTime = [self.componentImage.componentAllImagesTime stringByAppendingString:@", "];
                self.componentImage.componentAllImagesTime = [self.componentImage.componentAllImagesTime stringByAppendingString:image_clicked_time];
            }
        }
    }
    if(componentInfo.componentStatus == 1)
    {
        [cell.dirty_btn setBackgroundColor:[UIColor colorWithRed:(138/256.0) green:(138/256.0) blue:(138/256.0) alpha:1]];
    }
    else if(componentInfo.componentStatus == 2)
    {
        [cell.dirty_btn setBackgroundColor:[UIColor colorWithRed:(138/256.0) green:(138/256.0) blue:(138/256.0) alpha:1]];
    }
    else if(componentInfo.componentStatus == 3)
    {
        [cell.dirty_btn setBackgroundColor:[UIColor colorWithRed:(252/255.0) green:(78/255.0) blue:(59/255.0) alpha:1]];
    }
    else if(componentInfo.componentStatus == 4)
    {
        [cell.dirty_btn setBackgroundColor:[UIColor colorWithRed:(138/256.0) green:(138/256.0) blue:(138/256.0) alpha:1]];
    }
    if([componentInfo.componentScore intValue] == 1)
    {
        [cell.a_btn setImage:[UIImage imageNamed:@"radio_checked.png"]forState:UIControlStateNormal];
        [cell.b_btn setImage:[UIImage imageNamed:@"radio.png"]forState:UIControlStateNormal];
        [cell.c_btn setImage:[UIImage imageNamed:@"radio.png"]forState:UIControlStateNormal];
        [cell.d_btn setImage:[UIImage imageNamed:@"radio.png"]forState:UIControlStateNormal];
        [cell.e_btn setImage:[UIImage imageNamed:@"radio.png"]forState:UIControlStateNormal];
        [cell.f_btn setImage:[UIImage imageNamed:@"radio.png"]forState:UIControlStateNormal];
    }
    else if([componentInfo.componentScore intValue] == 2)
    {
        [cell.b_btn setImage:[UIImage imageNamed:@"radio_checked.png"]forState:UIControlStateNormal];
        [cell.a_btn setImage:[UIImage imageNamed:@"radio.png"]forState:UIControlStateNormal];
        [cell.c_btn setImage:[UIImage imageNamed:@"radio.png"]forState:UIControlStateNormal];
        [cell.d_btn setImage:[UIImage imageNamed:@"radio.png"]forState:UIControlStateNormal];
        [cell.e_btn setImage:[UIImage imageNamed:@"radio.png"]forState:UIControlStateNormal];
        [cell.f_btn setImage:[UIImage imageNamed:@"radio.png"]forState:UIControlStateNormal];
    }
    else if([componentInfo.componentScore intValue] == 3)
    {
        [cell.c_btn setImage:[UIImage imageNamed:@"radio_checked.png"]forState:UIControlStateNormal];
        [cell.b_btn setImage:[UIImage imageNamed:@"radio.png"]forState:UIControlStateNormal];
        [cell.a_btn setImage:[UIImage imageNamed:@"radio.png"]forState:UIControlStateNormal];
        [cell.d_btn setImage:[UIImage imageNamed:@"radio.png"]forState:UIControlStateNormal];
        [cell.e_btn setImage:[UIImage imageNamed:@"radio.png"]forState:UIControlStateNormal];
        [cell.f_btn setImage:[UIImage imageNamed:@"radio.png"]forState:UIControlStateNormal];
    }
    else if([componentInfo.componentScore intValue] == 4)
    {
        [cell.d_btn setImage:[UIImage imageNamed:@"radio_checked.png"]forState:UIControlStateNormal];
        [cell.b_btn setImage:[UIImage imageNamed:@"radio.png"]forState:UIControlStateNormal];
        [cell.c_btn setImage:[UIImage imageNamed:@"radio.png"]forState:UIControlStateNormal];
        [cell.a_btn setImage:[UIImage imageNamed:@"radio.png"]forState:UIControlStateNormal];
        [cell.e_btn setImage:[UIImage imageNamed:@"radio.png"]forState:UIControlStateNormal];
        [cell.f_btn setImage:[UIImage imageNamed:@"radio.png"]forState:UIControlStateNormal];
    }
    else if([componentInfo.componentScore intValue] == 5)
    {
        [cell.e_btn setImage:[UIImage imageNamed:@"radio_checked.png"]forState:UIControlStateNormal];
        [cell.b_btn setImage:[UIImage imageNamed:@"radio.png"]forState:UIControlStateNormal];
        [cell.c_btn setImage:[UIImage imageNamed:@"radio.png"]forState:UIControlStateNormal];
        [cell.d_btn setImage:[UIImage imageNamed:@"radio.png"]forState:UIControlStateNormal];
        [cell.a_btn setImage:[UIImage imageNamed:@"radio.png"]forState:UIControlStateNormal];
        [cell.f_btn setImage:[UIImage imageNamed:@"radio.png"]forState:UIControlStateNormal];
    }
    else if([componentInfo.componentScore intValue] == 6)
    {
        [cell.f_btn setImage:[UIImage imageNamed:@"radio_checked.png"]forState:UIControlStateNormal];
        [cell.b_btn setImage:[UIImage imageNamed:@"radio.png"]forState:UIControlStateNormal];
        [cell.c_btn setImage:[UIImage imageNamed:@"radio.png"]forState:UIControlStateNormal];
        [cell.d_btn setImage:[UIImage imageNamed:@"radio.png"]forState:UIControlStateNormal];
        [cell.e_btn setImage:[UIImage imageNamed:@"radio.png"]forState:UIControlStateNormal];
        [cell.a_btn setImage:[UIImage imageNamed:@"radio.png"]forState:UIControlStateNormal];
    }
    return cell;
}

请帮帮我。如何解决此案的内存问题?

1 个答案:

答案 0 :(得分:0)

您应首先在ViewDidLoad中注册此单元格,然后尝试将这些常用设置放在Cell (void)awakeFromNib方法中。