DreamWatcher
DreamWatcher

Reputation: 399

UITableView cell reuse, low Memory Warning, App Crashing in device after loading images

Please help me writing good code

My Scenario: loading image in tableview from custom image array(size can be 1-1000 or may be more)- Done
Placed Image on Button-Done (Dont know right way but working fine)
Getting Image tag with the help of button-Done
Not to load extra images in cell (suppose 4 image view in one cell and 26 images)-Done

I am Loading Images in UItableView but app only crashing in device, working fine in simulator. i have googled and found some answer on reusing cell modified my code accordingly to some extent. But i am still not sure about that image loading and crashing. Some time when the image are less it crash on scrolling and some time just after loading images in table view. I am close to my solution but not getting good result. Please Help!!
Please check my code and modify it if required

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

    NSString *CellIdentifier =[NSString stringWithFormat:@"%i-%i", indexPath.section,indexPath.row];
    //  static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell;

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
        NSInteger imageIndex = [indexPath row] * 4;
        NSInteger size = [imageArray count];

        imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80,80)];
        imageView1.image = [imageArray objectAtIndex:imageIndex];

        aButton1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [aButton1 setTag:imageIndex];
        [aButton1 setImage:imageView1.image forState:UIControlStateNormal];
        aButton1.frame = CGRectMake(0, 0, 80,80);

        [aButton1 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:aButton1];

        if (size == imageIndex +1) {
            return cell;
        }

        imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(80, 0, 80,80)];
        // imageView2.tag= tagValue+2000;
        imageView2.image = [imageArray objectAtIndex:imageIndex+1];
        [cell.contentView addSubview:imageView2];

        UIButton* aButton2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [aButton2 setTag:imageIndex+1];
        [aButton2 setImage:imageView2.image forState:UIControlStateNormal];
        aButton2.frame = CGRectMake(80, 0, 80,80);
        [aButton2 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:aButton2];

        if (size == imageIndex + 2) {
            return cell;
        }

        imageView3 = [[UIImageView alloc] initWithFrame:CGRectMake(160, 0, 80,80)];
        imageView3.image = [imageArray objectAtIndex:imageIndex+2];

        UIButton* aButton3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [aButton3 setTag:imageIndex+2];
        [aButton3 setImage:imageView3.image forState:UIControlStateNormal];
        aButton3.frame = CGRectMake(160, 0, 80,80);

        [aButton3 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:aButton3];

        if (size == imageIndex + 3) {
            return cell;
        }
        imageView4 = [[UIImageView alloc] initWithFrame:CGRectMake(240, 0, 80,80)];
        imageView4.image = [imageArray objectAtIndex:imageIndex+3];

        UIButton* aButton4 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [aButton4 setTag:imageIndex+3];
        [aButton4 setImage:imageView4.image forState:UIControlStateNormal];
        aButton4.frame = CGRectMake(240, 0, 80,80);
        [aButton4 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:aButton4];

    }

    else
    {
        NSLog(@"old one");
    }

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}

Upvotes: 2

Views: 1827

Answers (2)

anoop4real
anoop4real

Reputation: 7708

I think size of the image matters. Can you try loading with image of very small size say less than 5kb and see (just for testing purpose) ?

Upvotes: 0

Praveen S
Praveen S

Reputation: 10393

The problem is with this line

NSString *CellIdentifier =[NSString stringWithFormat:@"%i-%i", indexPath.section,indexPath.row];

Each of the row is created and none of the cells are reused except when scrolling back to already displayed cells.

A better way would be to take advantage of reuse of cells by using a identifier to give a cell back when it goes out of view and then styling it according to the requirement. More like NSString *CellIdentifier =@"MyCellIdentifier";

Now check the section and row for the cell and style it accordingly.

Upvotes: 4

Related Questions