Search code examples
iphoneuitableviewmemory-leaksuiimagedealloc

Signal "0" error while scrolling a tableview with images


I have a problem while scrolling images on tableview.

I am getting a Signal "0" error.

I think it is due to some memory issues but I am not able to find out the exact error. The code is as follows,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [travelSummeryPhotosTable dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) 
    {           
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];

    }

    //Photo ImageView
    UIImageView *photoTag = [[UIImageView alloc] initWithFrame:CGRectMake(5.0, 5.0, 85.0, 85.0)];

    NSString *rowPath =[[imagePathsDictionary valueForKey:[summaryTableViewDataArray objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];

    photoTag.image = [UIImage imageWithContentsOfFile:rowPath];
    [cell.contentView addSubview:photoTag];

    [photoTag release];

    // Image Caption
    UILabel *labelImageCaption = [[UILabel alloc] initWithFrame:CGRectMake(110.0, 15.0, 190.0, 50.0)];
    labelImageCaption.textAlignment = UITextAlignmentLeft;
    NSString *imageCaptionText =[   [imageCaptionsDictionary valueForKey:[summaryTableViewDataArray objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
    labelImageCaption.text = imageCaptionText;
    [cell.contentView addSubview:labelImageCaption];
    [labelImageCaption release];

    return cell;

}   

Thanks in advance.


Solution

  • Signal "0" error usually means that application crashed because of low memory.

    It seems that your problem is the following - you create cell subviews and add them to your cell each time cellForRowAtIndexPath method gets called - so all your allocated UI elements hang in memory and application gets out of it eventually.

    To solve your problem you must create cell's subviews only once - when creating it and later just setup their contents:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
        static NSString *CellIdentifier = @"Cell";
    
        UITableViewCell *cell = [travelSummeryPhotosTable dequeueReusableCellWithIdentifier:CellIdentifier];
    
        if (cell == nil) 
        {           
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
    
            UIImageView *photoTag = [[UIImageView alloc] initWithFrame:CGRectMake(5.0, 5.0, 85.0, 85.0)];
            photoTag.tag = 10;
            [cell.contentView addSubview:photoTag];
            [photoTag release];
    
            UILabel *labelImageCaption = [[UILabel alloc] initWithFrame:CGRectMake(110.0, 15.0, 190.0, 50.0)];
            labelImageCaption.tag = 11;
            labelImageCaption.textAlignment = UITextAlignmentLeft;
            [cell.contentView addSubview:labelImageCaption];
            [labelImageCaption release];
        }
    
        //Photo ImageView
        NSString *rowPath =[[imagePathsDictionary valueForKey:[summaryTableViewDataArray objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
        UIImageView* photoTag = (UIImageView*)[cell.contentView viewWithTag:10];
        photoTag.image = [UIImage imageWithContentsOfFile:rowPath];
    
        // Image Caption
        UILabel *labelImageCaption = (UILabel*)[cell.contentView viewWithTag:11];
        NSString *imageCaptionText =[   [imageCaptionsDictionary valueForKey:[summaryTableViewDataArray objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
        labelImageCaption.text = imageCaptionText;
    
        return cell;
    }