-3

在我的应用程序中,我加载了带有图像的表格视图。有一个删除按钮可以删除图像。

当图像尺寸太大(例如 1.5MB)时,应用程序会因内存不足警告而崩溃。

我如何使用仪器或其他解决方案解决这个问题?

如何在cellforRowIndexPath方法中释放对象。

我应该将项目设为启用 ARC 吗?

我是在做 ARC Enabled 项目,那会有什么效果?

我的 cellRow 代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    }

    for (UIView *v in [cell.contentView subviews]) {
        [v removeFromSuperview];
    }

    int section = indexPath.section;

    sectionItems = [sections objectAtIndex:section];

    int n = [sectionItems count];

    //NSLog(@"section items -%d",n );

    int  i=0;
    int i1=0;

    while(i<n)
    {
        int yy = 3 +i1*100;
        int j=0;
        for(j=0; j<3;j++){

            if (i>=n) break;
            Item *item = [sectionItems objectAtIndex:i];            

            //Imageview
            UIImageView *pp=[[UIImageView alloc]initWithFrame:CGRectMake(20+215*j, yy*2.3, 190, 190)];
            pp.image=item.img;
            //NSLog(@"%@ %@",item.img,item.image);
            //pp.backgroundColor=[UIColor redColor];
            [cell.contentView addSubview:pp];

            //Image Button
           // CGRect rect1 = CGRectMake(30+215*j, yy*2.3, 190, 250);
            CGRect rect1 = CGRectMake(20+215*j, yy*2.3, 190, 190);
            UIButton *button1=[[UIButton alloc] initWithFrame:rect1];
            [button1 setFrame:rect1];
            NSString *tagValue1 = [NSString stringWithFormat:@"%d%d", indexPath.section+1, i];
            button1.tag = [tagValue1 intValue];
            [button1 setBackgroundImage:nil forState:UIControlStateNormal];
            [button1 addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
           // button1.backgroundColor=[UIColor greenColor];
            [cell.contentView addSubview:button1];

           //delete button..16 03 2013
            CGRect rect2 = CGRectMake(176+215*j, yy*2.3, 30, 30);
            UIButton *button2=[[UIButton alloc] initWithFrame:rect2];
            [button2 setFrame:rect2];
            NSString *tagValue2 = [NSString stringWithFormat:@"%d%d", indexPath.section+1, i];
            button2.tag = [tagValue2 intValue];
            [button2 setBackgroundImage:[UIImage imageNamed:@"can.png"] forState:UIControlStateNormal];
            [button2 addTarget:self action:@selector(deleteItem:) forControlEvents:UIControlEventTouchUpInside];
            //button2.backgroundColor=[UIColor blackColor];


            [cell.contentView addSubview:button2];
            if (isDeleting == YES)
            {
                [button2 setHidden:NO];
                [button1 setUserInteractionEnabled:NO];
            }
            else
            {
                [button1 setUserInteractionEnabled:YES];
                [button2 setHidden:YES];
            }


            i++;
        }
        i1 = i1+1;
    }

    cell.selectionStyle=UITableViewCellSelectionStyleNone;

    return cell;
}
4

2 回答 2

0

嗯……嗯,首先:

pp 
button1
button2

已分配且从未释放...在您将它们添加为子视图之后开始为所有它们添加释放:

[pp release];
[button1 release];
[button2 release];

您当前的代码每次用户滚动时都会分配大量内存,并且永远不会释放...这可能会解决您的问题...

更进一步应该是重用对象,学习如何重用所有按钮和 imageView(只需为它们设置新内容,作为图像,而不是删除它们并重新创建它们;我知道单元格可能有不同数量的按钮......但是我相信你可以管理它..删除只是数量过多的对象,这可能会更好)

于 2013-05-29T14:22:44.713 回答
0

代替

for (UIView *v in [cell.contentView subviews]) {
        [v removeFromSuperview];
    }

尝试这个

[[cell.contentView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];

我认为您的编码是在 UIView *v 中复制视图并从 v 中删除 superView 而不是 [cell.contentView subViews]

于 2015-06-23T07:30:59.057 回答