gpt4 book ai didi

ios - UITableView 内存泄漏

转载 作者:行者123 更新时间:2023-11-29 03:26:05 27 4
gpt4 key购买 nike

解决方案

只需阅读@Kjuly的答案即可

非常感谢

问题

我将 tableView 与部分一起使用,每个部分有 4 行,第一行必须显示来 self 使用的网站的图像 HJCache类来缓存图像并避免泄漏/内存问题。

现在,这段代码运行良好,当我快速滚动时它不会泄漏或造成内存问题

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//UITableViewCell *cell=nil;

if (indexPath.row == 0) {
static NSString *CellIdentifier = @"Cell";
HJManagedImageV* mi;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];


mi = [[HJManagedImageV alloc] initWithFrame:CGRectMake(0,0,cell.frame.size.width, 310)];
mi.tag = 999;

[cell addSubview:mi];

} else {
mi = (HJManagedImageV*)[cell viewWithTag:999];
[mi clear];

}



if (indexPath.row == 0) {

mi.image = [UIImage imageNamed:@"placeholder"];

mi.url = [NSURL URLWithString:[pictures objectAtIndex:indexPath.section]];

[objMan manage:mi];
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(likeTappedDouble:)];
tapped.numberOfTapsRequired = 2;

[mi setUserInteractionEnabled:YES];
[mi addGestureRecognizer:tapped];



}

return cell;

}
}

但是,当我尝试配置其他行时,它会泄漏,并且在滚动应用程序时会出现内存问题并且速度非常慢。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//UITableViewCell *cell=nil;

if (indexPath.row == 0) {
static NSString *CellIdentifier = @"Cell";
HJManagedImageV* mi;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];


mi = [[HJManagedImageV alloc] initWithFrame:CGRectMake(0,0,cell.frame.size.width, 310)];
mi.tag = 999;

[cell addSubview:mi];

} else {
mi = (HJManagedImageV*)[cell viewWithTag:999];
[mi clear];

}



if (indexPath.row == 0) {

mi.image = [UIImage imageNamed:@"placeholder"];

mi.url = [NSURL URLWithString:[pictures objectAtIndex:indexPath.section]];

[objMan manage:mi];


UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(likeTappedDouble:)];
tapped.numberOfTapsRequired = 2;

[mi setUserInteractionEnabled:YES];
[mi addGestureRecognizer:tapped];



}

return cell;

}



static NSString *CellIdentifier = @"CellS";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];



if (indexPath.row == 1) {
// configure row 1
}
if (indexPath.row == 2) {
// configure row 2
}

// etc for the others ..

return cell;
}

问题出在哪里,谢谢..

更新

这段代码效果不佳,它在滚动时将 subview 添加到其他行

 static NSString *CellIdentifier = @"CellS";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;

if (indexPath.row == 1) {
UIImage* likesimage = [UIImage imageNamed:@"likespic"];
CGRect frameimg = CGRectMake(7, 5, likesimage.size.width, likesimage.size.height);
likesbutton = [[UIButton alloc] initWithFrame:frameimg];
[likesbutton setBackgroundImage:likesimage forState:UIControlStateNormal];
likesbutton.backgroundColor = [UIColor clearColor];
[cell addSubview:likesbutton];

label3 = [[UILabel alloc] initWithFrame:CGRectMake(20, 2, 100, 20)];
label3.textColor = [UIColor colorWithRed:61.0/255.0 green:113.0/255.0 blue:154.0/255.0 alpha:1.0];
label3.backgroundColor = [UIColor clearColor];
label3.font = [UIFont fontWithName:@"Helvetica-Bold" size:12];
label3.adjustsFontSizeToFitWidth = YES;

[cell addSubview:label3];



}

}

最佳答案

对于其他单元格,您需要重复使用该单元格:

static NSString *CellIdentifier = @"CellS";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

编辑更新的问题:

您需要了解“单元重用”的工作原理。至于你更新的代码,它说只有第一行需要像图像这样的 subview ,对吗?因此,您需要将其添加到 if (cell == nil){} 代码段之外,如下面的代码:

static NSString *CellIdentifier = @"CellS";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}

if (indexPath.row == 1) {
UIImage* likesimage = [UIImage imageNamed:@"likespic"];
CGRect frameimg = CGRectMake(7, 5, likesimage.size.width, likesimage.size.height);
likesbutton = [[UIButton alloc] initWithFrame:frameimg];
[likesbutton setBackgroundImage:likesimage forState:UIControlStateNormal];
likesbutton.backgroundColor = [UIColor clearColor];
[cell addSubview:likesbutton];

label3 = [[UILabel alloc] initWithFrame:CGRectMake(20, 2, 100, 20)];
label3.textColor = [UIColor colorWithRed:61.0/255.0 green:113.0/255.0 blue:154.0/255.0 alpha:1.0];
label3.backgroundColor = [UIColor clearColor];
label3.font = [UIFont fontWithName:@"Helvetica-Bold" size:12];
label3.adjustsFontSizeToFitWidth = YES;

[cell addSubview:label3];
}

注意:最好像对第 0 行所做的那样创建一个新的单元格实例,因为它只需要创建一次。

关于ios - UITableView 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20440681/

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