gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 20:40:52 24 4
gpt4 key购买 nike

请帮助我编写好的代码

我的场景:从自定义图像数组中加载 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com