0

I am trying to create a simple collection view with custom cells that have an UIImageView as a background. It's going to look something like :

enter image description here

This is my code :

ViewController.m


- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.collectionView registerClass:[MyCell class] forCellWithReuseIdentifier:@"MY_CELL"];
    leftInset = rightInset = 0;
    numberOfDays = 31;

    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 200;
}
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section
{
    return numberOfDays;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
    MyCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"MY_CELL" forIndexPath:indexPath];

    if(indexPath.row < 10)
    cell.imageView.image = [UIImage imageNamed:@"no"];
    else if(indexPath.section%3==1)
    {
        cell.imageView.image = [UIImage imageNamed:@"ok"];
    }
    else if(indexPath.section%3==2)
    {
        cell.imageView.image = [UIImage imageNamed:@"sa"];
    }
    else{
        cell.imageView.image = [UIImage imageNamed:@"happy_vote"];
    }
    return cell;
}

#pragma mark – UICollectionViewDelegateFlowLayout

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    return CGSizeMake(CELL_DIM, CELL_DIM);
}

- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    // top left bottom right
    return UIEdgeInsetsMake(5, leftInset, 0, 0);
}

MyCell.m

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        NSLog(@"Init with frame!");
        self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, frame.size.width, frame.size.height)];

        [self.contentView addSubview:self.imageView];
        [self.imageView setBackgroundColor:[UIColor redColor]];
    }
    return self;
}

It works fine for around 10 sections in the collection view. The problem is when I want to add more sections, the vertical scrolling starts to move really slow. I noticed that all the cells are allocated all at once. I am searching for a way to make this more resource friendly, and have the scrolling move continuously rather than slow and laggish. Could you tell me how to take care of the resources allocation to avoid this problem ? Thank you.

PS : I've tried removing the image view from the cell background and in the method cellForItemAtIndexPath: set a background color for the cell but again, after 10 or more sections it moves really slow.

Teo
  • 3,394
  • 11
  • 43
  • 73
  • What are the dimensions of the image resources you're using? – Stavash Jan 31 '13 at 10:28
  • The dimensions are 7 by 7 – Teo Jan 31 '13 at 11:18
  • Reason here: http://stackoverflow.com/questions/14453741/dequeuereusablecellwithidentifier-not-reusing-cells – LE SANG Feb 01 '13 at 15:12
  • solve: http://stackoverflow.com/questions/14287353/error-setting-text-in-collection-view-cell/14289862#14289862 Init your ImageView in your Cell, set cellImageView.image in setter for imageName, – LE SANG Feb 01 '13 at 15:14

1 Answers1

0

What you might want to do to help performance time is to check if an object is already initiated. You could also separately instantiate your images so that it doesnt have to be read from memory every time:

Make image1 a property of the view controller:

- (void)viewDidLoad
{
    image1 = [UIImage imageNamed:@"image1"];
    image2 = [UIImage imageNamed:@"image2"];
    image3 = [UIImage imageNamed:@"image3"];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;
 {
     MyCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"MY_CELL" forIndexPath:indexPath];

     if(indexPath.row < 10)
     cell.imageView.image = [UIImage imageNamed:@"no"];
     else if(indexPath.section%3==1)
     {
         cell.imageView.image = image1;
     }
     else if(indexPath.section%3==2)
     {
         cell.imageView.image = image2;
     }
     else{
         cell.imageView.image = image3;
     }
     return cell;
}
Michael Lorenzo
  • 628
  • 10
  • 20