gpt4 book ai didi

iphone - cellForRowAtIndexPath 内存管理

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:50:01 25 4
gpt4 key购买 nike

所以,这是我的 cellForRowAtIndexPath 的代码:

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

if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"identifier"]autorelease];
}

NSInteger artistIndex = 1; // 1
NSInteger albumIndex = 3; // 3
NSInteger dateIndex = 6; // 6
NSInteger imageIndex = 8; // 5

// ARTIST
CGRect frame = CGRectMake(59, 11, 244, 13);
UILabel *label = [[UILabel alloc]initWithFrame:frame];
label.font = [UIFont boldSystemFontOfSize:13];
label.textColor = [UIColor blackColor];
label.text = [[musicList.list objectAtIndex:indexPath.row] objectAtIndex:artistIndex];
[cell addSubview:label];

// ALBUM (more like description...
frame = CGRectMake(60, 30, 244, 11);
label = [[UILabel alloc]initWithFrame:frame];
label.font = [UIFont boldSystemFontOfSize:11];
label.textColor = [UIColor darkGrayColor];
label.text = [[musicList.list objectAtIndex:indexPath.row] objectAtIndex:albumIndex];
[cell addSubview:label];

// DATE
frame = CGRectMake(59, 49, 244, 10);
label = [[UILabel alloc]initWithFrame:frame];
label.font = [UIFont fontWithName:@"Helvetica" size:10.0];
label.textColor = [UIColor darkGrayColor];
label.textAlignment = UITextAlignmentRight;
label.text = [[musicList.list objectAtIndex:indexPath.row] objectAtIndex:dateIndex];
[cell addSubview:label];

// IMAGE
UIImageView *imageView = [[UIImageView alloc]init];
imageView.image = [[musicList.list objectAtIndex:indexPath.row] objectAtIndex:imageIndex];
imageView.frame = CGRectMake(8,9,44,44);
[imageView.layer setMasksToBounds:YES];
[imageView.layer setCornerRadius:3.0];
[[cell contentView] addSubview:imageView];

[imageView release];
[label release];

return cell;
}

为了解释发生了什么,基本上它只是利用一个数组来存储除索引 8(存储 UIImage)之外的所有内容的字符串。

我没有设置自动释放变量(主要是因为我还不太了解自动释放,哈哈)

无论如何,当在应用程序中滚动时,它会逐渐变慢(主要是因为 UIImage,但也部分是因为标签和框架)。

有没有办法更好地管理我的内存?另外,有没有更好的编码方式?我正在考虑制作一个 UITableViewCells 数组,然后通过 cellForRowAtIndexPath 访问它们。

所以我很乐意提供一些建议,谢谢大家。

最佳答案

你在那里泄露了很多标签。每次您分配/初始化一个新标签时,您都需要释放它。目前,您通过为它分配一个新的标签对象而不释放旧的标签对象来多次重复使用同一个标签变量。

不要将 [label release] 放在末尾,而是将 [label release] 放在每个 [cell addSubview:label] 之后;

您的下一个问题是您误解了表格单元格回收的工作原理。 UITableCell 对象在表格中一遍又一遍地重复使用,这就是为什么要这样做:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifier"];

if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"identifier"]autorelease];
}

这基本上是在说“检查是否有可用的单元格,如果没有则创建一个新单元格”。当一个单元格被重新使用时,它会保留你添加到其中的所有旧内容。因此,当您将这些标签和 ImageView 添加到单元格时,它们将在下次使用时保留在该单元格中,并在下一次使用。

但是因为每次重新使用单元格时都会再次添加标签,它会建立标签的多个副本,所以第二次显示单元格时,它的标签数量是原来的两倍,而下一次是标签数量的 3 倍许多,等等。您看不到重复项,因为它们位于完全相同的位置,但它们在那里会耗尽内存并减慢您的表速度。

您需要在“if (cell == nil) { ... }”语句中移动将新 View 附加到单元格的代码,以便在创建单元格时仅添加一次标签。

当然,这意味着每个单元格都将具有相同的文本和图像,这是您不想要的,因此您需要拆分设置文本和图像的逻辑并将其放在 if 语句之后。这样您只需创建一次标签,但每次它们显示在不同的表格行中时您都会设置文本。这有点棘手,因为您不再有对标签的引用,但您可以通过在标签上设置唯一标签来实现,这样您就可以再次找到它们。

这是一个清除了所有漏洞的清理版本:

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifier"];

NSInteger artistTag = 1;
NSInteger albumTag = 2;
NSInteger dateTag = 3;
NSInteger imageTag = 4;

if (cell == nil) {

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"identifier"]autorelease];

// ARTIST
CGRect frame = CGRectMake(59, 11, 244, 13);
UILabel *label = [[UILabel alloc]initWithFrame:frame];
label.font = [UIFont boldSystemFontOfSize:13];
label.textColor = [UIColor blackColor];
label.tag = artistTag;
[cell addSubview:label];
[label release];

// ALBUM (more like description...
frame = CGRectMake(60, 30, 244, 11);
label = [[UILabel alloc]initWithFrame:frame];
label.font = [UIFont boldSystemFontOfSize:11];
label.textColor = [UIColor darkGrayColor];
label.tag = albumTag;
[cell addSubview:label];
[label release];

// DATE
frame = CGRectMake(59, 49, 244, 10);
label = [[UILabel alloc]initWithFrame:frame];
label.font = [UIFont fontWithName:@"Helvetica" size:10.0];
label.textColor = [UIColor darkGrayColor];
label.textAlignment = UITextAlignmentRight;
label.tag = dateTag;
[cell addSubview:label];
[label release];

// IMAGE
UIImageView *imageView = [[UIImageView alloc]init];
imageView.frame = CGRectMake(8,9,44,44);
imageView.tag = imageTag;
[imageView.layer setMasksToBounds:YES];
[imageView.layer setCornerRadius:3.0];
[[cell contentView] addSubview:imageView];
[imageView release];
}

NSInteger artistIndex = 1; // 1
NSInteger albumIndex = 3; // 3
NSInteger dateIndex = 6; // 6
NSInteger imageIndex = 8; // 5

((UILabel *)[cell viewWithTag:artistTag]).text = [[musicList.list objectAtIndex:indexPath.row] objectAtIndex:artistIndex];

((UILabel *)[cell viewWithTag:albumTag]).text = [[musicList.list objectAtIndex:indexPath.row] objectAtIndex:albumIndex];

((UILabel *)[cell viewWithTag:dateTag]).text = [[musicList.list objectAtIndex:indexPath.row] objectAtIndex:dateIndex];

((UIImageView *)[cell viewWithTag:imageTag]).image = [[musicList.list objectAtIndex:indexPath.row] objectAtIndex:imageIndex];


return cell;
}

关于iphone - cellForRowAtIndexPath 内存管理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8859735/

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