在我的应用程序中,我加载了带有图像的表格视图。有一个删除按钮可以删除图像。
当图像尺寸太大(例如 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;
}