iphone - UITableView 单元格重用、低内存警告、加载图像后设备中的应用程序崩溃

标签 iphone objective-c xcode uitableview

请帮助我编写好的代码

我的场景: 从自定义图像数组中加载 tableview 中的图像(大小可以是 1-1000 或可能更多)- 完成
将图像放置在按钮完成上(不知道正确的方法,但工作正常)
使用按钮完成获取图像标签
不要在单元格中加载额外的图像(假设一个单元格中有 4 个 ImageView 和 26 个图像)-完成

我正在 UItableView 中加载图像,但应用程序仅在设备中崩溃,在模拟器中工作正常。我用谷歌搜索并找到了一些关于重用单元格的答案,并在某种程度上相应地修改了我的代码。但我仍然不确定图像加载和崩溃。有时当图像较少时,它会在滚动时崩溃,有时在 TableView 中加载图像后会崩溃。 我已经接近我的解决方案,但没有得到好的结果。请帮忙!!
请检查我的代码并根据需要进行修改

-(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;
}

最佳答案

问题出在这一行

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

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

更好的方法是利用单元格的重用,使用标识符在单元格退出 View 时将其返回,然后根据要求对其进行样式设置。更像 NSString *CellIdentifier =@"MyCellIdentifier";

现在检查单元格的部分和行并相应地设置其样式。

关于iphone - UITableView 单元格重用、低内存警告、加载图像后设备中的应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16229927/

相关文章:

iphone - 添加或删除 View 时,NSViewController 会收到通知吗?

objective-c - UITableView 阴影

ios - 设备发布时应用程序安装失败

ios - Xcode 11 XCUITest 无法获取匹配的快照 : Error getting main window kAXErrorServerNotFound

xcode - 如何在 Mac 上同时安装 ffmpeg 和应用程序?

ios - 使用配置文件禁用声音

iphone - 与 insertSubView 的索引值混淆

iphone - 并排安装 xcode 4.4 和 xcode 4.5?

ios - 使用新项目 iOS 上传新版本的应用程序

objective-c - 是否可以在 KVC 中使用通配符?