Note that there are some explanatory texts on larger screens.

plurals
  1. POMemory Leak UITableView
    primarykey
    data
    text
    <p><strong>SOLUTION</strong></p> <p>Just read the answer of <a href="https://stackoverflow.com/users/904365/kjuly">@Kjuly</a></p> <p>Thanks alot</p> <p><strong>QUESTION</strong></p> <p>I used tableView with sections, each section has 4 rows, the first row it must show image from website which I used <a href="https://github.com/markofjohnson/HJCache" rel="nofollow noreferrer">HJCache</a> class to cache the image and avoid leaking/memory issues.</p> <p>now, this code it works well and while I am scrolling fast it doesn't leak or make memory issue</p> <pre><code>- (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; } } </code></pre> <p>But when I try to configure other rows it leaked and while scrolling the application make memory issue and be very slowly.</p> <pre><code>- (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; } </code></pre> <p>Where is the problem, Thanks..</p> <p><strong>UPDATE</strong></p> <p>This code doesn't work well, it add the subview in other row while scrolling</p> <pre><code> 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]; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload