UITableView单元重用,低内存警告,应用程序在加载图像后崩溃

时间:2013-04-26 06:17:55

标签: iphone objective-c xcode uitableview

请帮我写好代​​码

我的情景: 从自定义图像数组加载tableview中的图像(大小可以是1-1000或更多) - 完成
按钮完成放置图像(不知道正确但工作正常)
借助按钮完成获取图像标签 不要在单元格中加载额外的图像(假设一个单元格中的4个图像视图和26个图像)-Done

我在UItableView中加载图片,但应用程序只在设备中崩溃,在模拟器中正常工作。我已经谷歌搜索,并在一定程度上找到了重新使用单元格修改我的代码的答案。但我仍然不确定该图像加载和崩溃。有些时候图像较少,它会在滚动时崩溃,有些时候只是在表格视图中加载图像后。 我接近我的解决方案但没有取得好成绩。请帮助!!
请检查我的代码并在必要时进行修改

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

    NSString *CellIdentifier =[NSString stringWithFormat:@"%i-%i", indexPath.section,indexPath.row];
    //  static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell;

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
        NSInteger imageIndex = [indexPath row] * 4;
        NSInteger size = [imageArray count];

        imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80,80)];
        imageView1.image = [imageArray objectAtIndex:imageIndex];

        aButton1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [aButton1 setTag:imageIndex];
        [aButton1 setImage:imageView1.image forState:UIControlStateNormal];
        aButton1.frame = CGRectMake(0, 0, 80,80);

        [aButton1 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:aButton1];

        if (size == imageIndex +1) {
            return cell;
        }

        imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(80, 0, 80,80)];
        // imageView2.tag= tagValue+2000;
        imageView2.image = [imageArray objectAtIndex:imageIndex+1];
        [cell.contentView addSubview:imageView2];

        UIButton* aButton2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [aButton2 setTag:imageIndex+1];
        [aButton2 setImage:imageView2.image forState:UIControlStateNormal];
        aButton2.frame = CGRectMake(80, 0, 80,80);
        [aButton2 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:aButton2];

        if (size == imageIndex + 2) {
            return cell;
        }

        imageView3 = [[UIImageView alloc] initWithFrame:CGRectMake(160, 0, 80,80)];
        imageView3.image = [imageArray objectAtIndex:imageIndex+2];

        UIButton* aButton3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [aButton3 setTag:imageIndex+2];
        [aButton3 setImage:imageView3.image forState:UIControlStateNormal];
        aButton3.frame = CGRectMake(160, 0, 80,80);

        [aButton3 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:aButton3];

        if (size == imageIndex + 3) {
            return cell;
        }
        imageView4 = [[UIImageView alloc] initWithFrame:CGRectMake(240, 0, 80,80)];
        imageView4.image = [imageArray objectAtIndex:imageIndex+3];

        UIButton* aButton4 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [aButton4 setTag:imageIndex+3];
        [aButton4 setImage:imageView4.image forState:UIControlStateNormal];
        aButton4.frame = CGRectMake(240, 0, 80,80);
        [aButton4 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:aButton4];

    }

    else
    {
        NSLog(@"old one");
    }

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}

2 个答案:

答案 0 :(得分:4)

问题在于这一行

NSString *CellIdentifier =[NSString stringWithFormat:@"%i-%i", indexPath.section,indexPath.row];

除了滚动回已经显示的单元格外,每行都会创建,并且不会重复使用任何单元格。

更好的方法是利用标识符重新利用单元格,以便在单元格离开视图时返回单元格,然后根据需要对单元格进行样式设置。更像是NSString *CellIdentifier =@"MyCellIdentifier";

现在检查单元格的section和row并相应地设置样式。

答案 1 :(得分:0)

我认为图像的大小很重要。您可以尝试加载小于5kb的非常小的图像并查看(仅用于测试目的)吗?