内存泄漏UITableView [英] Memory Leak UITableView

查看:102
本文介绍了内存泄漏UITableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决方案

只需阅读 @Kjuly

非常感谢

问题

我使用带有部分的tableView,每个部分有4行,第一行必须显示我使用过的网站的图像 HJCache 类可缓存图像并避免泄漏/内存问题.

I used tableView with sections, each section has 4 rows, the first row it must show image from website which I used HJCache class to cache the image and avoid leaking/memory issues.

现在,此代码可以正常工作,并且在我快速滚动时不会泄漏或导致内存问题

now, this code it works well and while I am scrolling fast it doesn't leak or make memory issue

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

    }
}

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

But when I try to configure other rows it leaked and while scrolling the application make memory issue and be very slowly.

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

问题出在哪里,谢谢.

更新

此代码无法正常运行,它会在滚动时在另一行添加子视图

This code doesn't work well, it add the subview in other row while scrolling

 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];



        }

    }

推荐答案

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

For your other cells, you need to reuse the cell either:

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


针对已更新的问题进行

您需要了解单元重用"的工作方式.至于您更新的代码,它说只有第1行需要像图像这样的子视图,对吗?因此,您需要将其添加到if (cell == nil){}代码段之外,例如下面的代码:


EDIT for Updated Question:

You need to know how "cell reusing" works. As for your updated code, it said only row 1 needs the subviews like image, right? So you need add it outside of the if (cell == nil){} snippet, like the code below:

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行一样创建一个新的单元格实例,因为只需要创建一次即可.

Note: It's better to create a new cell instance like what you did for row 0, cause it's only need to be created once.

这篇关于内存泄漏UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆